hi,
I recently discovered the _msize() function which returns the size of dynamically allocated memory, and decided to write a small test program to check it out. However instead of giving me the correct sizes of 10 and 20 it actually gives 16 and 24.
I decided to see what mingw 32-bit would give so I ran it in Codeblocks which did indeed return the correct values of 10 and 20.
(had to include <malloc.h> and change the format specifier from %zu to %u)
Anyone any ideas?
#include <stdio.h>
#include <stdlib.h>
//#include <malloc.h>
#define SIZE_ONE 10
#define SIZE_TWO 20
int main(void)
{
char* array_ptr;
char* temp;
int i;
size_t array_size;
array_ptr = malloc(sizeof(char) * SIZE_ONE);
if(array_ptr == NULL)
{
fprintf(stderr,"Malloc error - Out Of Memory.");
exit(EXIT_FAILURE);
}
array_size = _msize(array_ptr);
printf("malloc'd array size = %zu\n\n",array_size);
for(i = 0;i < SIZE_ONE;i++)
{
array_ptr[i] = '0' + i;
printf(" %c",array_ptr[i]);
}
printf("\n\n");
temp = array_ptr;
array_ptr = realloc(array_ptr,sizeof(char) * SIZE_TWO);
if(array_ptr == NULL)
{
fprintf(stderr,"Realloc error - Out Of Memory.");
free(temp);
exit(EXIT_FAILURE);
}
array_size = _msize(array_ptr);
printf("malloc'd array size = %zu\n\n",array_size);
for(i = SIZE_ONE;i < SIZE_TWO;i++)
{
array_ptr[i] = 'A' + i - SIZE_ONE;
printf(" %c",array_ptr[i]);
}
printf("\n\n");
for(i = 0;i < SIZE_TWO;i++)
{
printf(" %c",array_ptr[i]);
}
printf("\n\n");
return 0;
}