因此分配零字节的定义不明确,因此我想将0字节视为失败。这段代码能解决问题吗
#include <stdio.h>
#incude "xmalloc.h"
void *malloc_or exit(size_t nbytes, const char *file, int line){
void *x; //declarea void pointer
if ((x = malloc(nbytes)) == NULL){
fprintf(stderr. " %s: line %d: malloc(%zu) bytes failed", file , line, nbytes);
exit(EXIT_FAILURE);
} else
return x;
}
Run Code Online (Sandbox Code Playgroud)