LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   a.out too big (https://www.linuxquestions.org/questions/programming-9/a-out-too-big-185546/)

nibbler 05-25-2004 04:15 AM

a.out too big
 
Hi!

This code
Code:

int array[4000000]={0};

int main()
{
        return 0;
}

compiled with 'g++ temp.cpp' produces a.out file 15Mb big, which is the amount of memory to be allocated.

Code:

#include <stdlib.h>

int main()
{
        int *array=(int*)malloc(sizeof(int)*4000000);
        return 0;
}

a.out is now 11K.

Anyone knows why this is so?

I'm using Red Hat 9, gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).

Thanks in advance

jlliagre 05-25-2004 05:04 AM

Your example illustrates the difference between statically and dynamically allocated memory.
In the first example, the int array is already in the binary while in the second one, the array is created at runtime.


All times are GMT -5. The time now is 10:13 AM.