memory for global variable...

Discussion in 'C Programming' started by sam_cit@yahoo.co.in, May 2, 2007.

  1. Guest

    Hi Everyone,

    When is memory allocated for a global variable declared in sample.c
    file? Is it during compile time or loading time or linking time?

    And is it correct to understand that the resulting exe will have the
    memory for the global variable?

    Thanks in advance!!!
    , May 2, 2007
    #1
    1. Advertising

  2. said:

    > Hi Everyone,
    >
    > When is memory allocated for a global variable declared in sample.c
    > file? Is it during compile time or loading time or linking time?


    Conceptually speaking, the memory required by a file scope object is
    allocated before main() is called, and remains allocated until the
    program stops executing. There need not be a compile time (it might be
    an interpreter), a load time (the program might never run), or a link
    time (the program might be a standalone program for an embedded system,
    with no runtime library to link).

    > And is it correct to understand that the resulting exe will have the
    > memory for the global variable?


    That depends on what you mean by "exe", "have", "memory", and "global
    variable".

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 2, 2007
    #2
    1. Advertising

  3. Chris Dollin Guest

    Richard Heathfield wrote:

    > said:
    >
    >> Hi Everyone,
    >>
    >> When is memory allocated for a global variable declared in sample.c
    >> file? Is it during compile time or loading time or linking time?

    >
    > Conceptually speaking, the memory required by a file scope object is
    > allocated before main() is called, and remains allocated until the
    > program stops executing.


    Does it work just as well (I'm not saying that this is what the standards
    say) to say that the memory required by a file scope object is allocated
    before the first use of that object [1] and remains allocated until after
    the last use of that object?

    After all, no need to allocate memory for an object that's never used,
    nor to retain it once it never will be used again.

    [1] `&foo` uses `foo`.

    --
    "Who do you serve, and who do you trust?" /Crusade/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, May 2, 2007
    #3
  4. In article <>,
    <> wrote:

    > When is memory allocated for a global variable declared in sample.c
    >file? Is it during compile time or loading time or linking time?
    >
    >And is it correct to understand that the resulting exe will have the
    >memory for the global variable?


    I'm not familiar with "exe" files, but a common technique is to divide
    variables into those whose initial value is zero, and the rest. The
    zero ones are allocated in space which is set to zero when the program
    starts up, so they don't need to be stored in the executable file.
    The others are stored in an area copied in from the executable file.

    One way or another, the executable file has to identify the initial
    values. Treating zero as a special case is just an optimisation
    that's worthwhile because (a) it's very common to use zero as the
    initial value and (b) it's the default initial value for static and
    global variables in C.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, May 2, 2007
    #4
  5. Chris Dollin said:
    > Richard Heathfield wrote:

    <snip>
    >>
    >> Conceptually speaking, the memory required by a file scope object is
    >> allocated before main() is called, and remains allocated until the
    >> program stops executing.

    >
    > Does it work just as well (I'm not saying that this is what the
    > standards say) to say that the memory required by a file scope object
    > is allocated before the first use of that object [1] and remains
    > allocated until after the last use of that object?


    Yes. That's the "as if" rule, working hard as usual...

    <snip>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 2, 2007
    #5
    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. Bryan Parkoff
    Replies:
    2
    Views:
    6,466
  2. Replies:
    1
    Views:
    368
    Michael Ekstrand
    Aug 21, 2005
  3. Mohanasundaram
    Replies:
    44
    Views:
    1,032
    Keith Thompson
    Aug 24, 2004
  4. Replies:
    53
    Views:
    2,972
    David Thompson
    Feb 26, 2007
  5. jubelbrus
    Replies:
    5
    Views:
    596
    JohnQ
    Jul 20, 2007
Loading...

Share This Page