gpt4 book ai didi

c - 在 malloc 的内存上使用 sizeof()

转载 作者:太空狗 更新时间:2023-10-29 16:33:08 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
newbie questions about malloc and sizeof

我正在尝试将字符串读入程序。当我注意到字符串有时被损坏时,我尝试了以下代码:

 void *mallocated = malloc(100);
printf("sizeof(mallocated) = %d\n", sizeof(mallocated));

根据我的程序,mallocated 的大小是8,尽管我为它分配了 100 个字节。因此,每当我尝试存储一个超过 8 个字节的字符串时,第 8 个字节之后的所有内容有时都会消失。为什么会发生这种情况,我该如何预防?

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