Size of an executable for locally created arrays

Discussion in 'C++' started by against.inex@gmail.com, Jul 28, 2006.

  1. Guest

    Hi,

    The following code snippet when compiled using gcc 3.3.5 creates an
    executable which is 6.9 KB in size.

    int main(){
    int arr[1000000]={0};
    long arr2[100000]={0};
    double arr3[100000]={0};
    }

    However

    int main(){
    int arr[1000000]={1};
    long arr2[100000]={1};
    double arr3[100000]={1};
    }

    creates an executable which is 5 MB in size. Why is there such a huge
    difference when we change the initialization value? In the former all
    the elements are zero while in the latter case all except the first one
    are zero. In another forum someone mentioned that in the latter case
    the space for the arrays is allocated in the data segment while in the
    former case it is not. Is that the case? If so can anyone brief me
    about how the compiler decides where memory is allocated in such cases?
    Do let me know if this a just compiler issue and not related to C++ per
    se.

    Thanks
    Priya
    , Jul 28, 2006
    #1
    1. Advertising

  2. Vikram Guest

    >
    > int main(){
    > int arr[1000000]={1};
    > long arr2[100000]={1};
    > double arr3[100000]={1};
    > }
    >
    > creates an executable which is 5 MB in size.


    I tried on VC 6 compiler. The exe is around 32KB for both 0 and 1
    initializations.
    Vikram, Jul 28, 2006
    #2
    1. Advertising

  3. Hello,

    wrote:

    > cases? Do let me know if this a just compiler issue and not related to
    > C++ per se.


    This is just a compiler issue, i.e. a quality of implementation issue.
    E.g. gcc-4.1.1 compiles the code to around 7200 bytes in both cases.
    The problem must have been known.

    There are many ways for a compiler to establish the initialization. The
    most important rule for the compiler is the as-if rule. The compiler
    may do what it wants as long the code behaves like as if nothing
    special had been done.

    The most trivial one is doing it at compile time by putting all the data
    as required into the executable into the data segment. This uses lots
    of space in the executable but no extra code and no extra execution
    time.

    In the case of initialization to zero, it is possible to use the BSS
    segment, which means that the memory is not used up in the executable,
    but at execution time. The memory is taken from the OS initialized to
    all bits zero, at least for most UNIX systems. This won't work when
    initializing to something different from zero.

    The last alternative for non zero-initialized data is producing code to
    initialize the arrays at runtime using space allocated in the BSS. This
    is what gcc-4.1.1 does when looking at the resulting assembler code.

    There are even more opportunities if you think about combinations.

    For arrays of that size, it is clear that allocating memory in the
    executable would increase loading time a lot more then including a
    little more code to do the initialization.

    It is obvious, that a compiler has to decide by the size of the array,
    the types involved, and the optimization objective (space or time) to
    find the best among the possibilities. Most of the work done for
    gcc-4.1.1 involves improvements in optimization. This is one field
    where improvements were possible.

    Bernd Strieder
    Bernd Strieder, Jul 28, 2006
    #3
  4. Guest

    Thanks a lot Bernd Strieder. That was exactly what I was looking for
    :).

    Regards
    Priya
    , Jul 29, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade

    Why are the .dll's created locally?

    Rob Meade, Dec 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    299
    Kevin Spencer
    Dec 2, 2003
  2. scrodchunk

    gcc executable size vs. target hex file size?

    scrodchunk, Jul 5, 2004, in forum: C Programming
    Replies:
    2
    Views:
    401
    Stephen L.
    Jul 6, 2004
  3. Replies:
    15
    Views:
    486
    Dave Thompson
    Aug 21, 2006
  4. pratap
    Replies:
    20
    Views:
    758
    Flash Gordon
    Mar 7, 2007
  5. Lionel van den Berg
    Replies:
    3
    Views:
    355
    Lionel van den Berg
    Oct 30, 2007
Loading...

Share This Page