【发布时间】:2019-01-28 02:30:20
【问题描述】:
我知道malloc(size_t size) 分配 size 字节并返回一个指向已分配内存的指针。 .
那么为什么当我分配零字节给整数指针 p 时,我仍然能够初始化它呢?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = malloc(0);
*p = 10;
printf("Pointer address is: %p\n", p);
printf("The value of the pointer: %d\n", *p);
return 0;
}
这是我的程序输出,我期待一个分段错误。
Pointer address is: 0x1ebd260
The value of the pointer: 10
【问题讨论】: