对于许多问题,答案似乎可以在"标准"中找到.但是,我们在哪里找到它?最好是在线.
谷歌搜索有时会觉得徒劳,尤其是对于C标准,因为他们在编程论坛的大量讨论中被淹没.
要开始这个,因为这些是我现在正在搜索的,那里有很好的在线资源:
在这种情况下realloc会失败吗?
int *a = NULL;
a = calloc(100, sizeof(*a));
printf("1.ptr: %d\n", a);
a = realloc(a, 50 * sizeof(*a));
printf("2.ptr: %d\n", a);
if(a == NULL){
printf("Is it possible?\n");
}
return (0);
Run Code Online (Sandbox Code Playgroud)
}
我的输出是:
1.ptr: 4072560
2.ptr: 4072560
Run Code Online (Sandbox Code Playgroud)
所以'a'指向相同的地址.那么我应该强制执行realloc检查吗?
稍后编辑:
稍后编辑 2:检查这种方式可以吗?
int *a = NULL, *b = NULL;
a = calloc(100, sizeof(*a));
b = realloc(a, 50 * sizeof(*a));
if(b == NULL){
return a;
}
a = b;
return a;
Run Code Online (Sandbox Code Playgroud) 如果做下一个:
int* array = malloc(10 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)
他们使用realloc:
array = realloc(array, 5 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)
在第二行(并且只有它),它可以返回NULL吗?