My question is: what's the difference between those 2 lines:
int ptr[4046];
bzero(ptr, 4046);
int *ptr;
ptr = (int *)malloc(sizeof(int) * 4046);
bzero(ptr, 4046);
I ask this because when I want to print the ptr
, the first one print some 0 and then garbage (random numbers) and the second (the malloc
one) print only 0, like I would like to. I use printf
to print it, like this:
int i = 0;
while (i++ < 4046)
printf("%x", ptr[i]);
EDIT: Thanks to Everyone, Answer: the size is of a int is not 1 byte but 4 bytes. So the first 4046 byte is valid and after I print what's the computer can find on the stack.