为什么malloc的初始化值设置为0的GCC? [英] Why does malloc initialize the values to 0 in gcc?

查看:153
本文介绍了为什么malloc的初始化值设置为0的GCC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是从不同的平台之间,但

Maybe it is different from platform to platform, but

当我编译使用gcc和运行下面的code,我在我的Ubuntu 11.10每次都遇到0。

when I compile using gcc and run the code below, I get 0 every time in my ubuntu 11.10.

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

int main()
{
    double *a = (double*) malloc(sizeof(double)*100)
    printf("%f", *a);
}

为什么malloc的这样的表现即使有释放calloc?

Why do malloc behave like this even though there is calloc?

不就意味着有不必要的性能开销仅仅值初始化为0,即使你不希望它是有时?

Doesn't it mean that there is an unwanted performance overhead just to initialize the values to 0 even if you don't want it to be sometimes?

编辑:哦,我的previous例如没有initiazling,但碰巧使用鲜块

Oh, my previous example was not initiazling, but happened to use "fresh" block.

我precisely一直在寻找的是为什么它分配一个大块它初始化它:

What I precisely was looking for was why it initializes it when it allocates a large block:

int main()
{
    int *a = (int*) malloc(sizeof(int)*200000);
    a[10] = 3;
    printf("%d", *(a+10));

    free(a);

    a = (double*) malloc(sizeof(double)*200000);
    printf("%d", *(a+10));
}

OUTPUT: 3
        0 (initialized)

但感谢指出有mallocing当安全理由! (从来没有想过它)。当然它有分配新鲜块,或大块时初始化为零。

But thanks for pointing out that there is a SECURITY reason when mallocing! (Never thought about it). Sure it has to initialize to zero when allocating fresh block, or the large block.

推荐答案

答案很简单:

这不,它只是恰巧是在您的案件为零。结果(同时测试的情况下不显示数据为零。只显示,如果一个元素是零。)

It doesn't, it just happens to be zero in your case.
(Also your test case doesn't show that the data is zero. It only shows if one element is zero.)

龙答:

当你调用的malloc(),两件事情将会发生:

When you call malloc(), one of two things will happen:


  1. 它回收存储器,这是previous分配并从相同的过程释放。

  2. 从操作系统请求新的页面(S)。

在第一种情况下,内存将包含从previous分配数据剩。所以它不会是零。小型演出分配时,这是通常的情况。

In the first case, the memory will contain data leftover from previous allocations. So it won't be zero. This is the usual case when performing small allocations.

在第二种情况下,该存储器将是从操作系统。这种情况发生在程序运行内存不足 - 或者当你要求一个非常大的分配。 (这是在您的示例的情况下)

In the second case, the memory will be from the OS. This happens when the program runs out of memory - or when you are requesting a very large allocation. (as is the case in your example)

下面的渔获:内存从OS未来将归零为的安全的原因,*

Here's the catch: Memory coming from the OS will be zeroed for security reasons.*

在操作系统给你的内存,它可能是从一个不同的进程中解脱出来。使得存储器可能包含敏感信息,例如一个密码。因此,要prevent你读这样的数据,它给它之前的OS将零吧。

When the OS gives you memory, it could have been freed from a different process. So that memory could contain sensitive information such as a password. So to prevent you reading such data, the OS will zero it before it gives it to you.

<子> *我注意到C标准只字未提这一点。这是严格意义上的操作系统行为。因此,这可能清零或者安全不是一个问题可能无法在系统present。

为了让更多的表演背景的:

正如@R。提到在评论,这归零就是为什么你应该总是使用释放calloc()而不是的malloc() + memset的() 释放calloc()可以利用这一点优势,避免单独的 memset的()

As @R. mentions in the comments, this zeroing is why you should always use calloc() instead of malloc() + memset(). calloc() can take advantage of this fact to avoid a separate memset().

在另一方面,本归零有时性能瓶颈。在某些数值应用程序(如外的地方FFT ),你需要分配临时内存的大块。用它来执行任何的算法,然后释放它。

On the other hand, this zeroing is sometimes a performance bottleneck. In some numerical applications (such as the out-of-place FFT), you need to allocate a huge chunk of scratch memory. Use it to perform whatever algorithm, then free it.

<青霉>在这些情况下,调零是不必要的,相当于纯开销。的

我见过的最极端的例子是一个48 GB的临时缓冲区70秒操作的20秒回零开销。 (大约30%的开销)。
<子>(授予:机器确实有缺少内存带宽)

The most extreme example I've seen is a 20-second zeroing overhead for a 70-second operation with a 48 GB scratch buffer. (Roughly 30% overhead.) (Granted: the machine did have a lack of memory bandwidth.)

显而易见的解决方案是简单地手动重新使用存储器。但是,这往往需要通过建立接口断裂。 (尤其是如果它是一个图书馆日常工作的一部分)

The obvious solution is to simply reuse the memory manually. But that often requires breaking through established interfaces. (especially if it's part of a library routine)

这篇关于为什么malloc的初始化值设置为0的GCC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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