malloc的归零内存? [英] malloc zeroing out memory?

查看:169
本文介绍了malloc的归零内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这款C code使用GCC编译4.3.3

Given this C code compiled with gcc 4.3.3

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


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

    int * i;

    i = (int *) malloc(sizeof(int));

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

}

我期望的输出是什么是在内存中的malloc()返回,而是输出为0。malloc的清零它返回的内存?如果是这样,为什么?

I would expect the output to be whatever was in the memory that malloc() returns, but instead the output is 0. Is malloc zeroing out the memory it returns? If so, why?

推荐答案

的malloc 本身不零出内存,但它许多操作系统将零内存,你的程序出于安全原因,请求(保留访问被另一个进程使用潜在的敏感信息的一个过程)。

malloc itself doesn't zero out memory but it many operating systems will zero the memory that your program requests for security reasons (to keep one process from accessing potentially sensitive information that was used by another process).

这篇关于malloc的归零内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆