Heap allocation experiment
Various compilers use different amount of memory spaces in heaps. As I compiled programs in GCC, MinGW GCC, and Microsoft Visual C++, I found different result. At a 100 byte char array, Visual C++ 2008 left 126 bytes before allocating another array, MinGW GCC left 112 bytes and in Ubuntu 8.10 Hardy, the built in GCC left 104 bytes. I used an P4 (which is a 686, supports EM64T), with 1536 MBs of RAM. And I got the mentioned results. My experiment was this:
`#include <stdio.h> #include <stdlib.h> #define MAX 100
int main(int argc, char *argv[]){
int i;
FILE *output;
output=fopen("data_hack1","w");
fprintf(output,"char
");
for(i=1; i<=MAX; i++){
char array1=(char)malloc(i);
char array2=(char)malloc(1);
fprintf(output,"The free space for %d length int array is %d
",i,array2-array1);
}
fprintf(output,"
");
return 0; } ` You may compile and see the result yourself in the file named data.txt. You should also note what compiler and system you are using. You may also send me the result, by email ([email protected]) or commenting this post.
You can also try out this, and see the difference between allocating int and char.
`#include <stdio.h> #include <stdlib.h> #define MAX 100
int main(int argc, char *argv[]){
int i;
FILE *output;
output=fopen("data_hack2","w");
for(i=1; i<=MAX; i++){
int array1=(int)malloc(i);
int array2=(int)malloc(1);
fprintf(output,"The free space for %d length int array is %d
",i,array2-array1);
}
fprintf(output,"
");
return 0;
}
`
This is also a program, which you can try out. In Ubuntu 8.10 GCC, for a 100 byte array, the program left about 26 bytes. I don't know why! I haven't tried this one out in other OS and compilers yet.