I am using Debian squeeze and have noticed that memory is always zeroed. Is this new in linux distributions ? Some time ago, I believe I could use puts() and garbage would b
As already illustrated, the key difference is first time allocation vs. allocation. If you try:
char *a, tst;
do {
a = malloc(50000000);
a[49999999] = '\0';
printf("%50s\n%p", a, a); // it outputs nothing 1st, but bbbb.... 2nd
tst = a[5000]
memset(a, 'b', 50000000);
free(a);
} while (tst == '\0');
it'll print you two lines (most likely, at least if the pointers are the same).
Key is that the memory block returned by malloc() has undefined contents. It may or may not be zeroes, and depends on how memory allocation has been done in the past by the program (or what memory debugging facilities are used).
If you want to guarantee contents, you need calloc() or explicit initialization after allocation.
The system's integrity / data separation guarantee on the other hand means that any initial address space requested by the system - whether via sbrk() or mmap(MAP_ANON) - must be zero-initialized, as any other contents of such would consist of a security breach.