小编Jos*_*osh的帖子

使用malloc分配0字节

因此分配零字节的定义不明确,因此我想将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)

c malloc dynamic-memory-allocation

0
推荐指数
1
解决办法
707
查看次数

标签 统计

c ×1

dynamic-memory-allocation ×1

malloc ×1