Is malloc() initializing allocated array to zero?

前端 未结 8 1078
无人及你
无人及你 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: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.

提交回复
热议问题