Is malloc() initializing allocated array to zero?

前端 未结 8 1047
无人及你
无人及你 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条回答
  •  萌比男神i
    2020-12-16 20:38

    From the C Standard 7.22.3.4:

    Synopsis

    #include 
    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.

提交回复
热议问题