谁能解释一下为什么会出错?
#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;
}
//不易发生错误的内存分配器实现
以下是头文件
#pragma once
#include <stdlib.h>
#define malloc
#define MALLOC(num,type)(type *)alloc((num)*sizeof(type))
extern void* alloc(size_t size);