在我的代码中,几乎每个函数都有一个或多个malloc调用,每次我必须执行以下操作:
char *ptr = (char *)malloc(sizeof(char) * some_int);
if (ptr == NULL) {
fprintf(stderr, "failed to allocate memory.\n");
return -1;
}
Run Code Online (Sandbox Code Playgroud)
这是额外的四行代码,如果我在使用malloc后每次添加它们,我的代码长度会增加很多..那么有一种优雅的方法来处理这个问题吗?
非常感谢!!
c ×1