Hello All,
All I am trying here writting wrapper for calloc() (IAR ARM compiler ) so that I can place size of allocated memory in the first 4 bytes of the memory and use the save while freeing the memory. This is to monitor heap size!
I'm facing a strange problem in Calloc() Wrapper which I tried. belwo is the code:
Code:
void *MyCalloc(unsigned long bytes)
{
bytes = ((bytes + 3) & ~0x3); //dont know why !?
bytes = bytes+4;
printf("bytes = %ld\n", bytes);
unsigned int *p = (unsigned int *)calloc(1, bytes);
printf("MyCallc @p == %p\n", p);
if (p == NULL)
{
printf(" MyCalloc: unable to allocate memory\n");
return NULL;
}
*((unsigned long*)p) = 0;
*((unsigned long*)p) = bytes;
return p+4;
}
output:
bytes = 264
MyCallc @p == 70128470
bytes = 16
MyCallc @p == 70128580
bytes = 16
MyCallc @p == 00000000
MyCalloc: unable to allocate memory
I have lot of heap memory left , dont know why its returning NULL.
below code is working fine:
Code:
void *ptr = (void *)calloc(1, (size_t)((bytes + 3) & ~0x3));
Thanks in advance .
-Ashok