gpt4 book ai didi

c - 我怎么能初始化一个分配了零字节的指针呢?

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:36 24 4
gpt4 key购买 nike

<分区>

我知道 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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com