172*_*729 0 c malloc macos free
我在Linux Mint过去几周一直在学习C语言.但是,我切换到OSX并测试了这段代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr = calloc(10, sizeof(int));
if(arr == NULL)
printf("arr is null");
for(int i = 0; i < 10; i++, arr++){
printf("%d \n", *arr);
}
free(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这应该工作并打印10个零,它会这样做,但它不会释放arr.
这是输出:
0
0
0
0
0
0
0
0
0
0
a.out(532,0x7fff72606300) malloc: *** error for object 0x7ff060c04b48: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)
我不明白我如何访问未分配的内存.如果没有分配arr,输出不应该是"arr为空"吗?