I wrote a code to test to stress test the memory management of Linux and Windows OS. Just for further tests I went ahead and checked what values are present in the memory returned by malloc().
The values that are being return are all 0 (zero). I have read the man page of malloc, checked on both Windows and Linux, but I am not able to find the reason for this behavior. According to the manpage the
The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized.
To clear the memory segment, one has to manually use memset().
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdbool.h> int eat(long total,int chunk){ long i; for(i=0;i<total;i+=chunk){ short *buffer=malloc(sizeof(char)*chunk); if(buffer==NULL){ return -1; } printf("\nDATA=%d",*buffer); memset(buffer,0,chunk); } return 0; } int main(int argc, char *argv[]){ int i,chunk=1024; long size=10000; printf("Got %ld bytes in chunks of %d...\n",size,chunk); if(eat(size,chunk)==0){ printf("Done, press any key to free the memory\n"); getchar(); }else{ printf("ERROR: Could not allocate the memory"); } } Maybe I am missing something. The code is adapted from here
EDIT: The problem has been been answered here for the GCC specific output. I believe Windows operating system would be also following the same procedures.