I would like to ask you a question. I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define XXX 1024*1024
int main()
{
int *p;
unsigned long x=0;
while (1)
{
//p = (int *) calloc (1,XXX);
p = (int *) malloc (XXX);
memset (p,0,XXX);
x++;
printf ("%lu MB allocated.\n",x);
sleep (1);
}
return 0;
}
If I run this code, everything runs as normal. Every second, a new MB is allocated in the memory. The problem I encounter is if I uncomment the calloc() line and comment the malloc() and memset() lines. From what I know, calloc() should initialize all bytes to zero in the allocated memory; the same thing that malloc() and memset() do.
When I run the code with calloc() (without the malloc() and memset()), an initial 1 MB is allocated (as it is normal), and then after some seconds (~10) another MB is allocated.
Why is this behaviour?
Thanks in advance!