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\n");
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\n",i,array2-array1);
}
fprintf(output,"\n\n");

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 (rivalslayer@yahoo.co.in) 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\n",i,array2-array1);
}
fprintf(output,"\n\n");
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.

Advertisements

One comment on “Heap allocation experiment

  1. […] did this experiment on July, 2008 and totally forgot about it. I was looking for some of my old experiments and found […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s