不一发生错误的内存分配器

alloc.h//接口

#include<stdlib.h>

#define malloc

#define MALLOC(num,type) (type *)alloc((num)*sizeof(type))

extern void *alloc(size_t size);

alloc.c//实现

#include <stdio.h>

#include "alloc.h"

#undef malloc

void * alloc(size_t size)

{

void *new_mem;

new_mem=malloc(size);

if(new_mem==NULL){

printf("out of memory\n");

exit(1);

}

return new_mem;

}

a_client.c//使用

#include "alloc.h"

void function(){

int *new_memory;

new_memory=MALLOC(25,int);

}

原文地址:https://www.cnblogs.com/macula7/p/1960571.html