C语言 使用 malloc 分配新内存后是否必须调用 memset

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13410550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:26:48  来源:igfitidea点击:

Do I have to call memset after I allocated new memory using malloc

cmallocmemset

提问by steave

#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
    int *test = malloc(15 * sizeof(int));
    for(int i = 0;i < 15 ;i  ++ )
        printf("test is %i\n",test[i]);

    memset(test,0,sizeof(int) * 15);

    for(int i = 0 ; i < 15; i ++ )
        printf("test after memset is %i\n",test[i]);

    return 0;
}

The output I get is very weird:

我得到的输出很奇怪:

    test is 1142126264
    test is 32526
    ...
    test is 1701409394
    test is 1869348978
    test is 1694498930
    test after memset is 0
    test after memset is 0
    test after memset is 0
    test after memset is 0
    test after memset is 0
    ...
    test after memset is 0
    test after memset is 0
    test after memset is 0
    test after memset is 0
    test after memset is 0

Why would that happen? I thought I just malloced some new fresh memory that is ready to use?

为什么会这样?我以为我刚刚malloc编写了一些可以使用的新内存?

So how about this:

那么这个怎么样:

int test[15];

Do I have to call memset(&test,0,sizeof(int) * 15);?

我必须打电话 memset(&test,0,sizeof(int) * 15);吗?

回答by 1''

mallocdoes not initialize the memory it allocates. You just get whatever random garbage was already in there. If you really need everything set to 0, use callocat a performance penalty. (If you need to initialize to something other than 0, use memsetfor byte arrays and otherwise manually loop over the array to initialize it.)

malloc不初始化它分配的内存。你只会得到那里已经存在的任何随机垃圾。如果您确实需要将所有内容都设置为 0,请calloc在性能损失下使用。(如果您需要初始化为 0 以外的值,请使用memsetfor 字节数组,否则手动循环数组以对其进行初始化。)

回答by Lundin

C11 7.22.3.4

C11 7.22.3.4

void *malloc(size_t size);

The malloc function allocates space for an object whose sizeis specified by size and whose value is indeterminate.

malloc 函数为大小由 size 指定且值不确定的对象分配空间。

If you want the values to be set to zero, use callocinstead. callocis basically just a wrapper function around one call to mallocand one call to memset(with value to set is 0).

如果您希望将值设置为零,请calloc改用。calloc基本上只是一个围绕一个调用malloc和一个调用的包装函数memset(要设置的值为 0)。

回答by Jay

When you request for a memory from heap, heap will just allocate any block of memory available to it. This block of memory may have some data depending upon a previous write.

当您从堆中请求内存时,堆只会分配任何可用的内存块。根据先前的写入,该内存块可能包含一些数据。

回答by jdigital

For performance reasons, malloc() makes no guarantee regarding the contents of newly allocated memory. It might be zeros, it might be random data, it might be anything. If you want malloc'ed memory to have a specific value, then it is up to you do it.

出于性能原因, malloc() 不保证新分配内存的内容。它可能是零,可能是随机数据,也可能是任何东西。如果您希望 malloc 的内存具有特定值,则由您决定。