Heap Allocation Experiment

I did this experiment on July, 2008 and totally forgot about it. I was looking for some of my old experiments and found this. It’s quite a nice little experiment.

It’s about comparing C compilers. I simply made program allocate some memory space for a certain array and allocated some memory again and measured the difference in pointer values (the memory distance).

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100

int main(int argc, char *argv[]){
    int i;
    FILE *output;
    output=fopen("results.txt","w");
    fprintf(output,"char array\n");
    for(i=1; i<=MAX; i++){
             char *array1=(char*)malloc(i * sizeof(char));
             char *array2=(char*)malloc(sizeof(char));
             fprintf(output,"The free space for %d length 'char' array is %ld\n",i,array2-array1);
             free(array1);
             free(array2);
    }
    return 0;
}

I modified the original program a little and compiled with various compilers and got the results. The results are stored in result.txt.

I tested it on three different compiler/OS combinations.

MinGW (gcc (GCC) 3.4.5 (mingw-vista special r3)) on Windows 7 64 bit/Core i3 370M:

char array
The free space for 1 length 'char' array is 7376
The free space for 2 length 'char' array is 7376
The free space for 3 length 'char' array is 7376
The free space for 4 length 'char' array is 7376
The free space for 5 length 'char' array is 7376
The free space for 6 length 'char' array is 7376
The free space for 7 length 'char' array is 7376
The free space for 8 length 'char' array is 7376
The free space for 9 length 'char' array is -7376
The free space for 10 length 'char' array is -7376
The free space for 11 length 'char' array is -7376
The free space for 12 length 'char' array is -7376
The free space for 13 length 'char' array is -7376
The free space for 14 length 'char' array is -7376
The free space for 15 length 'char' array is -7376
The free space for 16 length 'char' array is -7376
The free space for 17 length 'char' array is -7376
The free space for 18 length 'char' array is -7376
The free space for 19 length 'char' array is -7376
The free space for 20 length 'char' array is -7376
The free space for 21 length 'char' array is -7376
The free space for 22 length 'char' array is -7376
The free space for 23 length 'char' array is -7376
The free space for 24 length 'char' array is -7376
The free space for 25 length 'char' array is -7376
The free space for 26 length 'char' array is -7376
The free space for 27 length 'char' array is -7376
The free space for 28 length 'char' array is -7376
The free space for 29 length 'char' array is -7376
The free space for 30 length 'char' array is -7376
The free space for 31 length 'char' array is -7376
The free space for 32 length 'char' array is -7376
The free space for 33 length 'char' array is -7376
The free space for 34 length 'char' array is -7376
The free space for 35 length 'char' array is -7376
The free space for 36 length 'char' array is -7376
The free space for 37 length 'char' array is -7376
The free space for 38 length 'char' array is -7376
The free space for 39 length 'char' array is -7376
The free space for 40 length 'char' array is -7376
The free space for 41 length 'char' array is -7376
The free space for 42 length 'char' array is -7376
The free space for 43 length 'char' array is -7376
The free space for 44 length 'char' array is -7376
The free space for 45 length 'char' array is -7376
The free space for 46 length 'char' array is -7376
The free space for 47 length 'char' array is -7376
The free space for 48 length 'char' array is -7376
The free space for 49 length 'char' array is -7376
The free space for 50 length 'char' array is -7376
The free space for 51 length 'char' array is -7376
The free space for 52 length 'char' array is -7376
The free space for 53 length 'char' array is -7376
The free space for 54 length 'char' array is -7376
The free space for 55 length 'char' array is -7376
The free space for 56 length 'char' array is -7376
The free space for 57 length 'char' array is -7376
The free space for 58 length 'char' array is -7376
The free space for 59 length 'char' array is -7376
The free space for 60 length 'char' array is -7376
The free space for 61 length 'char' array is -7376
The free space for 62 length 'char' array is -7376
The free space for 63 length 'char' array is -7376
The free space for 64 length 'char' array is -7376
The free space for 65 length 'char' array is -7376
The free space for 66 length 'char' array is -7376
The free space for 67 length 'char' array is -7376
The free space for 68 length 'char' array is -7376
The free space for 69 length 'char' array is -7376
The free space for 70 length 'char' array is -7376
The free space for 71 length 'char' array is -7376
The free space for 72 length 'char' array is -7376
The free space for 73 length 'char' array is -7376
The free space for 74 length 'char' array is -7376
The free space for 75 length 'char' array is -7376
The free space for 76 length 'char' array is -7376
The free space for 77 length 'char' array is -7376
The free space for 78 length 'char' array is -7376
The free space for 79 length 'char' array is -7376
The free space for 80 length 'char' array is -7376
The free space for 81 length 'char' array is -7376
The free space for 82 length 'char' array is -7376
The free space for 83 length 'char' array is -7376
The free space for 84 length 'char' array is -7376
The free space for 85 length 'char' array is -7376
The free space for 86 length 'char' array is -7376
The free space for 87 length 'char' array is -7376
The free space for 88 length 'char' array is -7376
The free space for 89 length 'char' array is -7376
The free space for 90 length 'char' array is -7376
The free space for 91 length 'char' array is -7376
The free space for 92 length 'char' array is -7376
The free space for 93 length 'char' array is -7376
The free space for 94 length 'char' array is -7376
The free space for 95 length 'char' array is -7376
The free space for 96 length 'char' array is -7376
The free space for 97 length 'char' array is -7376
The free space for 98 length 'char' array is -7376
The free space for 99 length 'char' array is -7376
The free space for 100 length 'char' array is -7376

Visual C++ 2010 (Microsoft Visual Studio 2010 Version 10.0.30319.1 RTMRel Microsoft .NET Framework Version 4.0.30319 RTMRel) on Windows 7 64 bit/Core i3 370M:

char array
The free space for 1 length 'char' array is 64
The free space for 2 length 'char' array is 64
The free space for 3 length 'char' array is 64
The free space for 4 length 'char' array is 64
The free space for 5 length 'char' array is 72
The free space for 6 length 'char' array is 72
The free space for 7 length 'char' array is 72
The free space for 8 length 'char' array is 72
The free space for 9 length 'char' array is 72
The free space for 10 length 'char' array is 72
The free space for 11 length 'char' array is 72
The free space for 12 length 'char' array is 72
The free space for 13 length 'char' array is 80
The free space for 14 length 'char' array is 80
The free space for 15 length 'char' array is 80
The free space for 16 length 'char' array is 80
The free space for 17 length 'char' array is 80
The free space for 18 length 'char' array is 80
The free space for 19 length 'char' array is 80
The free space for 20 length 'char' array is 80
The free space for 21 length 'char' array is 88
The free space for 22 length 'char' array is 88
The free space for 23 length 'char' array is 88
The free space for 24 length 'char' array is 88
The free space for 25 length 'char' array is 88
The free space for 26 length 'char' array is 88
The free space for 27 length 'char' array is 88
The free space for 28 length 'char' array is 88
The free space for 29 length 'char' array is 96
The free space for 30 length 'char' array is 96
The free space for 31 length 'char' array is 96
The free space for 32 length 'char' array is 96
The free space for 33 length 'char' array is 96
The free space for 34 length 'char' array is 96
The free space for 35 length 'char' array is 96
The free space for 36 length 'char' array is 96
The free space for 37 length 'char' array is 104
The free space for 38 length 'char' array is 104
The free space for 39 length 'char' array is 104
The free space for 40 length 'char' array is 104
The free space for 41 length 'char' array is 104
The free space for 42 length 'char' array is 104
The free space for 43 length 'char' array is 104
The free space for 44 length 'char' array is 104
The free space for 45 length 'char' array is 112
The free space for 46 length 'char' array is 112
The free space for 47 length 'char' array is 112
The free space for 48 length 'char' array is 112
The free space for 49 length 'char' array is 112
The free space for 50 length 'char' array is 112
The free space for 51 length 'char' array is 112
The free space for 52 length 'char' array is 112
The free space for 53 length 'char' array is 120
The free space for 54 length 'char' array is 120
The free space for 55 length 'char' array is 120
The free space for 56 length 'char' array is 120
The free space for 57 length 'char' array is 120
The free space for 58 length 'char' array is 120
The free space for 59 length 'char' array is 120
The free space for 60 length 'char' array is 120
The free space for 61 length 'char' array is 128
The free space for 62 length 'char' array is 128
The free space for 63 length 'char' array is 128
The free space for 64 length 'char' array is 128
The free space for 65 length 'char' array is 128
The free space for 66 length 'char' array is 128
The free space for 67 length 'char' array is 128
The free space for 68 length 'char' array is 128
The free space for 69 length 'char' array is 136
The free space for 70 length 'char' array is 136
The free space for 71 length 'char' array is 136
The free space for 72 length 'char' array is 136
The free space for 73 length 'char' array is 136
The free space for 74 length 'char' array is 136
The free space for 75 length 'char' array is 136
The free space for 76 length 'char' array is 136
The free space for 77 length 'char' array is 144
The free space for 78 length 'char' array is 144
The free space for 79 length 'char' array is 144
The free space for 80 length 'char' array is 144
The free space for 81 length 'char' array is 144
The free space for 82 length 'char' array is 144
The free space for 83 length 'char' array is 144
The free space for 84 length 'char' array is 144
The free space for 85 length 'char' array is 152
The free space for 86 length 'char' array is 152
The free space for 87 length 'char' array is 152
The free space for 88 length 'char' array is 152
The free space for 89 length 'char' array is 152
The free space for 90 length 'char' array is 152
The free space for 91 length 'char' array is 152
The free space for 92 length 'char' array is 152
The free space for 93 length 'char' array is 160
The free space for 94 length 'char' array is 160
The free space for 95 length 'char' array is 160
The free space for 96 length 'char' array is 160
The free space for 97 length 'char' array is 160
The free space for 98 length 'char' array is 160
The free space for 99 length 'char' array is 160
The free space for 100 length 'char' array is 160

g++ (gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)) on Fedora 14 32 bit/ Core i3 370M:

char array
The free space for 1 length 'char' array is 16
The free space for 2 length 'char' array is -16
The free space for 3 length 'char' array is 16
The free space for 4 length 'char' array is -16
The free space for 5 length 'char' array is 16
The free space for 6 length 'char' array is -16
The free space for 7 length 'char' array is 16
The free space for 8 length 'char' array is -16
The free space for 9 length 'char' array is 16
The free space for 10 length 'char' array is -16
The free space for 11 length 'char' array is 16
The free space for 12 length 'char' array is -16
The free space for 13 length 'char' array is -32
The free space for 14 length 'char' array is -32
The free space for 15 length 'char' array is -32
The free space for 16 length 'char' array is -32
The free space for 17 length 'char' array is -32
The free space for 18 length 'char' array is -32
The free space for 19 length 'char' array is -32
The free space for 20 length 'char' array is -32
The free space for 21 length 'char' array is -56
The free space for 22 length 'char' array is -56
The free space for 23 length 'char' array is -56
The free space for 24 length 'char' array is -56
The free space for 25 length 'char' array is -56
The free space for 26 length 'char' array is -56
The free space for 27 length 'char' array is -56
The free space for 28 length 'char' array is -56
The free space for 29 length 'char' array is -88
The free space for 30 length 'char' array is -88
The free space for 31 length 'char' array is -88
The free space for 32 length 'char' array is -88
The free space for 33 length 'char' array is -88
The free space for 34 length 'char' array is -88
The free space for 35 length 'char' array is -88
The free space for 36 length 'char' array is -88
The free space for 37 length 'char' array is -128
The free space for 38 length 'char' array is -128
The free space for 39 length 'char' array is -128
The free space for 40 length 'char' array is -128
The free space for 41 length 'char' array is -128
The free space for 42 length 'char' array is -128
The free space for 43 length 'char' array is -128
The free space for 44 length 'char' array is -128
The free space for 45 length 'char' array is -176
The free space for 46 length 'char' array is -176
The free space for 47 length 'char' array is -176
The free space for 48 length 'char' array is -176
The free space for 49 length 'char' array is -176
The free space for 50 length 'char' array is -176
The free space for 51 length 'char' array is -176
The free space for 52 length 'char' array is -176
The free space for 53 length 'char' array is -232
The free space for 54 length 'char' array is -232
The free space for 55 length 'char' array is -232
The free space for 56 length 'char' array is -232
The free space for 57 length 'char' array is -232
The free space for 58 length 'char' array is -232
The free space for 59 length 'char' array is -232
The free space for 60 length 'char' array is -232
The free space for 61 length 'char' array is -296
The free space for 62 length 'char' array is -16
The free space for 63 length 'char' array is -16
The free space for 64 length 'char' array is -16
The free space for 65 length 'char' array is -16
The free space for 66 length 'char' array is -16
The free space for 67 length 'char' array is -16
The free space for 68 length 'char' array is -16
The free space for 69 length 'char' array is -16
The free space for 70 length 'char' array is -16
The free space for 71 length 'char' array is -16
The free space for 72 length 'char' array is -16
The free space for 73 length 'char' array is -16
The free space for 74 length 'char' array is -16
The free space for 75 length 'char' array is -16
The free space for 76 length 'char' array is -16
The free space for 77 length 'char' array is -16
The free space for 78 length 'char' array is -16
The free space for 79 length 'char' array is -16
The free space for 80 length 'char' array is -16
The free space for 81 length 'char' array is -16
The free space for 82 length 'char' array is -16
The free space for 83 length 'char' array is -16
The free space for 84 length 'char' array is -16
The free space for 85 length 'char' array is -16
The free space for 86 length 'char' array is -16
The free space for 87 length 'char' array is -16
The free space for 88 length 'char' array is -16
The free space for 89 length 'char' array is -16
The free space for 90 length 'char' array is -16
The free space for 91 length 'char' array is -16
The free space for 92 length 'char' array is -16
The free space for 93 length 'char' array is -16
The free space for 94 length 'char' array is -16
The free space for 95 length 'char' array is -16
The free space for 96 length 'char' array is -16
The free space for 97 length 'char' array is -16
The free space for 98 length 'char' array is -16
The free space for 99 length 'char' array is -16
The free space for 100 length 'char' array is -16

The comparison makes clear that the old MinGW version of gcc is quite inefficient in allocating memories as far as two consecutive malloc()s are concerned. On the other hand other two compilers are quite good, and Fedora version of GCC exhibits a strange pattern. I didn’t note the kernel version of the Fedora 14 I have installed. I may test it on other systems, modify this experiment and post the results.

Readers are welcome to post their results.

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s