Thread: Memory Allocation

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    Memory Allocation

    In my lecture notes it says
    Code:
    double array[1000000]; //should take 8Mbytes
    When in fact this does not make an executable of 8mb, this it tells me though it does not say why and i was not at the lecture for part of this topic.
    Code:
    double array[1000000] = {1.0, 2.0 ...}; // this makes the file ~8mb
    Why doesnt this make the file larger?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Such are the hazards of missing out on bits of technical subjects...

    > double array[1000000]; //should take 8Mbytes
    Goes into the uninitialised data segment (called BSS in the Unix world). Although that sounds bad, C guarantees that bss is filled with zeros when the program starts
    One good thing about the BSS segment is that it doesn't take up space in the program image. The program just contains a linker instruction which says "I want X bytes of BSS'. The program loader reads this, allocates it, zeros it and hands it over to your program.

    > double array[1000000] = {1.0, 2.0 ...};
    This goes into the initialised data segment. Since this is loaded at the same time as the program is loaded, it takes up space in the program image.

    Notice how the 8MB shifts from the bss to the data segment when the array is initialised
    Code:
    + cat hello.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    double a[1000000]
    #ifdef INIT
    = { 1, 2 }
    #endif
    ;
    
    int main ( ) {
        return 0;
    }
    + gcc hello.c
    + size a.out
       text    data     bss     dec     hex filename
        604     256 8000032 8000892  7a157c a.out
    + ls -l a.out
    -rwxrwxr-x    1 spc      spc         11274 Jun 12 11:25 a.out
    + gcc -DINIT hello.c
    + size a.out
       text    data     bss     dec     hex filename
        604 8000276       4 8000884  7a1574 a.out
    + ls -l a.out
    -rwxrwxr-x    1 spc      spc       8011290 Jun 12 11:25 a.out
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Thanks for that in-depth reply, Much clearer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM