Is malloc() initializing allocated array to zero?

前端 未结 8 1035
无人及你
无人及你 2020-12-16 20:31

Here is the code I\'m using:

#include 
#include 

int main() {
    int *arr;
    int sz = 100000;
    arr = (int *)malloc(sz *         


        
相关标签:
8条回答
  • 2020-12-16 20:38

    From the C Standard 7.22.3.4:

    Synopsis

    #include <stdlib.h>
    void *malloc(size_t size);
    

    Description

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

    The value is indeterminate. So, every compiler is free to behave how it wants. For example, in Microsoft Visual C++, in Debug mode, the area of allocated memory by malloc() is all set to 0xCDCDCDCD and when in Release mode it is random. In modern versions of GCC, it is set to 0x000000 if you don't enable code optimizations, and random otherwise. I don't know about other compilers, but you get the idea.

    0 讨论(0)
  • 2020-12-16 20:39

    malloc isn't supposed to initialize the allocated memory to zero. Why is this happening?

    This is how it was designed more than 40 years ago.

    But, on the same time, there was created the calloc() function that initializes the allocated memory to zero and it's the recommended way to allocate memory for arrays.

    The line:

    arr = (int *)malloc(sz * sizeof(int));
    

    Should read:

    arr = calloc(sz, sizeof(int));
    

    If you are learning C from an old book it teaches you to always cast the value returned by malloc() or calloc() (a void *) to the type of the variable you assign the value to (int * in your case). This is obsolete, if the value returned by malloc() or calloc() is directly assigned to a variable, the modern versions of C do not need that cast any more.

    0 讨论(0)
  • 2020-12-16 20:46

    The man page of malloc says:

    The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

    So, malloc() returns uninitialized memory, the contents of which is indeterminate.

     if (arr[i] != 0)
    

    In your program, You have tried to access the content of a memory block, which is invoked undefined behavior.

    0 讨论(0)
  • 2020-12-16 20:47

    void *malloc(size_t size) is just supposed to keep aside the specified amount of space. That's all. There is no guarantee as to what will be present in that space.

    Quoted from the man pages:

    The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

    Apart from calloc() you can use the memset() function to zero out a block of memory.

    0 讨论(0)
  • 2020-12-16 20:48

    Zero's are assigned to page contents at first time in linux kernel.

    Below program explains the memory initialisation difference in malloc and calloc:

    #include<stdio.h>
    #include<stdlib.h>
    
    #define SIZE 5
    
    int main(void) {
        int *mal = (int*)malloc(SIZE*sizeof(int));
        int *cal = (int*)calloc(SIZE, sizeof(int));
    
        mal[4] = cal[4] = 100;
    
        free(mal); free(cal);
    
        mal = (int*)malloc(SIZE*sizeof(int));
        cal = (int*)calloc(SIZE, sizeof(int));
    
        for(int i=0; i<SIZE; i++) {
            printf("mall[%d] = %d\n", i, mal[i]);
        }
        for(int i=0; i<SIZE; i++) {
            printf("call[%d] = %d\n", i, cal[i]);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:49

    The first time you call malloc(3), it asks to the operating system to get memory for the heap space.

    For security reasons, the unix/linux kernel (and many other operating systems) in general zeroes the page contents that is to be given to a process, so no process can access that memory's previous contents and do nasty things with it (like searching for old passwords, or similar things).

    If you do several allocations and deallocations of memory, when the malloc module reuses the previous memory, you'll see garbage coming from malloc(3).

    0 讨论(0)
提交回复
热议问题