> As long as all-bytes-zero means zero for your variable type (or NULL for your pointer type)
Question 7.31
Weird machines are few and far between.
Question 5.17
It might be hard to see anything from Click_here's example.
Memory is often (always, because you know - privacy and security) cleared to zeros by the OS, so the first time you call malloc, it often reads as zeros anyway.
If it isn't cleared by the OS, your library malloc might still clear it (as mine does).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int i, setSize = 10;
char *apple;
apple = malloc(setSize * sizeof(*apple));
printf("Ptr=%p",(void*)apple);
if (apple != NULL)
{
printf("\nmalloc: ");
for (i=0; i<setSize; i++)
{
printf("0x%x ", apple[i]);
}
}
strcpy(apple,"Secret");
free(apple);
// That was fun, let's do it again
apple = malloc(setSize * sizeof(*apple));
printf("\nPtr=%p",(void*)apple);
if (apple != NULL)
{
printf("\nmalloc: ");
for (i=0; i<setSize; i++)
{
printf("0x%x ", apple[i]);
}
}
strcpy(apple,"Secret");
free(apple);
apple = calloc(setSize, sizeof(*apple));
printf("\nPtr=%p",(void*)apple);
if (apple != NULL)
{
printf("\ncalloc: ");
for (i=0; i<setSize; i++)
{
printf("0x%x ", apple[i]);
}
}
free(apple);
fflush(stdout);
return 0;
}
$ ./a.out
Ptr=0x1dd3010
malloc: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
Ptr=0x1dd3010
malloc: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
Ptr=0x1dd3010
calloc: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
Each alloc comes back with the same address each time, but there is never any trace of the previous contents.