tags:

views:

378

answers:

5

Given this C code compiled with gcc 4.3.3

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


int main(int argc, char * argv[])
{

    int * i;

    i = (int *) malloc(sizeof(int));

    printf("%d\n", *i);
    return 0;

}

I would expect the output to be whatever was in the memory that malloc() returns, but instead the output is 0. Is malloc zeroing out the memory it returns? If so, why?

+5  A: 

The malloc() function does not set the allocated memory to any specific value. If you want to make sure the memory is zero, use calloc() or equivalent. Otherwise, you get whatever what was there before (which might, in your case, be zero).

Greg Hewgill
I would expect that, if it wasn't consistently 0. Why would I get a value that is consistently 0, over and over again?
endeavormac
You're running the same program in the same way repeatedly and expecting different results? Although `malloc()` itself is not guaranteed to zero out the memory, the OS is doing the same work to load your program and set it running. Your runtime library code is doing the same thing to set up the heap memory and begin running your code. As mentioned in another answer, the OS is likely to give you new memory pages initialised to some specific value, in order to isolate different processes from one another.
Greg Hewgill
Your program is requesting fresh memory from the OS. The OS will zeroout memory when it's first assigned to a process. Try filling in random values, free() the piece, and allocate another one.
nos
A: 

You definitely can't depend on it being 0. malloc a larger chunk and dump it to see.

pixelbeat
+10  A: 

malloc itself doesn't zero out memory but it many operating systems will zero the memory that your program requests for security reasons (to keep one process from accessing potentially sensitive information that was used by another process).

Robert Gamble
Thanks, this gives me somewhere to look for the specific answer!
endeavormac
I should mention that nothing in the standard *prevents* malloc from zeroing its memory. For efficiency, it *usually* doesn't but this isn't mandated.
paxdiablo
Also note that while new memory from the OS may be zeroed, if your application recycles memory that it was using previously it's likely _not_ to be zeroed. So don't even think about relying on this :)
bdonlan
A: 

The value in the allocated memory is officially undefined. C99 states: The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate. malloc() can do whatever it wants, including zeroing it out. This may be deliberate, a side-effect of the implementation, or you might just have a lot of memory that happens to be 0.

FWIW on OS X with Apple's gcc 4.0.1 I can't make it come out not 0 even doing a lot of allocations:

for( idx = 0; idx < 100000; idx++ ) {
    i = (int *) malloc(sizeof(int));
    printf("%d\n", *i);
}
Schwern
Try setting *i to nonzero, freeing i, then mallocing again and see what you get.
Stephen Canon
+1  A: 

Malloc does not have to fill memory. For performance reasons, the release versions often do nothing with memory. A "secure" library may have a malloc implementation which clears memory. It is up to the library implementer. There are some common patterns that are filled into memory by various compiler's debug libraries that are explained in this stackoverflow topic.

Adisak