此代码显示“不成功”,但如果将 -1 替换为 0,则它不会为 NULL。我不明白如何分配 0 内存空间。我知道没有用但不是 NULL == 0L 所以它也应该是 == 0 ..
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
if((ptr = malloc(-1)) == NULL)
printf("unsuccessful: no memory space was allocated.\n");
else{
printf("successful: memoryspace was allocated. \n");
free(ptr);
}
getch();
return 0;
}