gpt4 book ai didi

c - malloc 清零内存?

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

给定这个用 gcc 4.3.3 编译的 C 代码

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char * argv[])
{

int * i;

i = malloc(sizeof(int));

printf("%d\n", *i);
return 0;

}

我希望输出是 malloc() 返回的内存中的任何内容,但输出是 0。malloc 是否将它返回的内存归零?如果是,为什么?

最佳答案

malloc 本身不会将内存归零,但出于安全原因,许多操作系统会将您的程序请求的内存归零(以防止一个进程访问另一个进程使用的潜在敏感信息).

关于c - malloc 清零内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622196/

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