Global Variables : Where are they stored ?

Discussion in 'C Programming' started by Kislay, Nov 27, 2007.

  1. Kislay

    Kislay Guest

    Which of the following is correct regarding the storage of global
    variables :
    1. Global variables exist in a memory area that exists from before the
    first reference
    in a program until after the last reference .
    2.Global variables exist in the initialized data segment .
     
    Kislay, Nov 27, 2007
    #1
    1. Advertising

  2. Kislay

    santosh Guest

    In article
    <>,
    Kislay <> wrote on Tuesday 27 Nov 2007 8:40 pm:

    > Which of the following is correct regarding the storage of global
    > variables :
    > 1. Global variables exist in a memory area that exists from before the
    > first reference in a program until after the last reference .


    The C standard says that file scope objects are created and initialised
    just before program start-up and exist till the program terminates. So
    your statement above is untrue.

    > 2.Global variables exist in the initialized data segment .


    The C standard does not define a "segment" or a "data segment". It says
    however that file scope objects (with or without external linkage) and
    those declared as static are initialised once prior to program
    start-up. If no explicit initialiser is provided, they are initialised
    to zero.
     
    santosh, Nov 27, 2007
    #2
    1. Advertising

  3. Kislay

    pete Guest

    Kislay wrote:
    >
    > Which of the following is correct regarding the storage of global
    > variables :
    > 1. Global variables exist in a memory area that exists from before the
    > first reference
    > in a program until after the last reference .
    > 2.Global variables exist in the initialized data segment .


    The aspect of memory that is special to what most people mean
    by "Global variables" is that the objects have static duration.
    Such objects are initialized before main starts executing
    and persist to the termination of the program.

    "data segment" may or may not be part of the way
    that C is implemented on your machine,
    But "data segment"
    isn't a concept of the C programming language.

    --
    pete
     
    pete, Nov 27, 2007
    #3
  4. Kislay

    James Kuyper Guest

    santosh wrote:
    > In article
    > <>,
    > Kislay <> wrote on Tuesday 27 Nov 2007 8:40 pm:
    >
    >> Which of the following is correct regarding the storage of global
    >> variables :
    >> 1. Global variables exist in a memory area that exists from before the
    >> first reference in a program until after the last reference .

    >
    > The C standard says that file scope objects are created and initialised
    > just before program start-up and exist till the program terminates. So
    > your statement above is untrue.


    Is there anyway for a strictly conforming program to detect whether the
    memory exists before the first time that it is referenced? Similarly, is
    there anyway for to detect whether the memory still exists after the
    last reference to it? I'm not aware of any way to do so, so the as-if
    rule would make these two statements about object lifetime compatible
    with each other.
     
    James Kuyper, Nov 27, 2007
    #4
  5. Kislay

    Richard Guest

    santosh <> writes:

    > In article
    > <>,
    > Kislay <> wrote on Tuesday 27 Nov 2007 8:40 pm:
    >
    >> Which of the following is correct regarding the storage of global
    >> variables :
    >> 1. Global variables exist in a memory area that exists from before the
    >> first reference in a program until after the last reference .

    >
    > The C standard says that file scope objects are created and
    > initialised


    Whats all this "file scope" you are bringing into it?

    > just before program start-up and exist till the program terminates. So
    > your statement above is untrue.
    >
    >> 2.Global variables exist in the initialized data segment .

    >
    > The C standard does not define a "segment" or a "data segment". It says
    > however that file scope objects (with or without external linkage) and
    > those declared as static are initialised once prior to program
    > start-up. If no explicit initialiser is provided, they are initialised
    > to zero.
     
    Richard, Nov 27, 2007
    #5
  6. santosh wrote:
    > In article
    > <>,
    > Kislay <> wrote on Tuesday 27 Nov 2007 8:40 pm:
    >
    >> Which of the following is correct regarding the storage of global
    >> variables :
    >> 1. Global variables exist in a memory area that exists from before the
    >> first reference in a program until after the last reference .

    >
    > The C standard says that file scope objects are created and initialised
    > just before program start-up and exist till the program terminates. So
    > your statement above is untrue.


    If we take it to be literally true, then his statement is a weaker form
    of yours. If an object is created before program start-up then it exists
    from before the first reference, and similarly if it exists until the
    program terminates then it exists until after the last reference. The OP
    did not give any information about /how long/ before the first reference
    or after the last reference the memory continues to exist.
     
    Philip Potter, Nov 27, 2007
    #6
  7. Kislay

    santosh Guest

    In article <%JW2j.37359$Xg.9360@trnddc06>, James Kuyper
    <> wrote on Tuesday 27 Nov 2007 9:00 pm:

    > santosh wrote:
    >> In article
    >> <>,
    >> Kislay <> wrote on Tuesday 27 Nov 2007 8:40
    >> pm:
    >>
    >>> Which of the following is correct regarding the storage of global
    >>> variables :
    >>> 1. Global variables exist in a memory area that exists from before
    >>> the first reference in a program until after the last reference .

    >>
    >> The C standard says that file scope objects are created and
    >> initialised just before program start-up and exist till the program
    >> terminates. So your statement above is untrue.

    >
    > Is there anyway for a strictly conforming program to detect whether
    > the memory exists before the first time that it is referenced?
    > Similarly, is there anyway for to detect whether the memory still
    > exists after the last reference to it?


    I don't think so, but the Standard nevertheless does not talk about the
    lifetime of static objects in terms of references to them. It says:

    6.2.4 Storage durations of objects

    3.
    An object whose identifier is declared with external or internal
    linkage, or with the storage-class specifier static has static storage
    duration. Its lifetime is the entire execution of the program and its
    stored value is initialized only once, prior to program startup

    As you can see, it says that the lifetime is the entire execution of the
    program, not merely till a reference to it exists.

    > I'm not aware of any way to do so, so the as-if rule would make these
    > two statements about object lifetime compatible with each other.


    I don't have enough knowledge with C to disprove your statement above,
    but perhaps someone else can.

    At least for the case of a volatile static object the lifetime has to
    continue till program termination and not depend upon any references to
    it existing or not.
     
    santosh, Nov 27, 2007
    #7
  8. Kislay

    Ivanna Pee Guest

    On Nov 27, 10:10 am, Kislay <> wrote:
    > Which of the following is correct regarding the storage of global
    > variables :
    > 1. Global variables exist in a memory area that exists from before the
    > first reference
    > in a program until after the last reference .
    > 2.Global variables exist in the initialized data segment .


    My global variables are stored in my keyboard.

    int hereIam;

    see? there is another one!
     
    Ivanna Pee, Nov 27, 2007
    #8
  9. Richard <> writes:
    > santosh <> writes:
    >> In article
    >> <>,
    >> Kislay <> wrote on Tuesday 27 Nov 2007 8:40 pm:
    >>
    >>> Which of the following is correct regarding the storage of global
    >>> variables :
    >>> 1. Global variables exist in a memory area that exists from before the
    >>> first reference in a program until after the last reference .

    >>
    >> The C standard says that file scope objects are created and
    >> initialised

    >
    > Whats all this "file scope" you are bringing into it?


    "File scope" is a term defined by the C standard. "Global variable"
    is not. It's likely that what the OP meant by "global variables" is
    variables (objects) declared at file scope, which therefore have
    static storage duration.

    Since C separates the concepts of visibility and lifetime, the phrase
    "global variables" could be potentially ambiguous. It's important to
    be clear about just what we're talking about.

    (No, I'm not saying that C doesn't have global variables, merely that
    it doesn't use that particular term for them.)

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 27, 2007
    #9
  10. Kislay

    Kislay Guest

    #include<stdio.h>
    int global_var ; // The Global Variable I am talking about
    int main()
    {
    return 0;
    }
    Thank you all for the above posts but it still doesn't clear my
    doubt . Where does a global variable , like the one declared in the
    program , global_var gets stored , i.e. which part of memory ?
     
    Kislay, Nov 28, 2007
    #10
  11. Kislay

    Ian Collins Guest

    Kislay wrote:
    > #include<stdio.h>
    > int global_var ; // The Global Variable I am talking about
    > int main()
    > {
    > return 0;
    > }
    > Thank you all for the above posts but it still doesn't clear my
    > doubt . Where does a global variable , like the one declared in the
    > program , global_var gets stored , i.e. which part of memory ?


    There isn't a standard answer, how memory is partitioned is an
    implementation detail.

    --
    Ian Collins.
     
    Ian Collins, Nov 28, 2007
    #11
  12. Kislay

    James Kuyper Guest

    Kislay wrote:
    > #include<stdio.h>
    > int global_var ; // The Global Variable I am talking about
    > int main()
    > {
    > return 0;
    > }
    > Thank you all for the above posts but it still doesn't clear my
    > doubt . Where does a global variable , like the one declared in the
    > program , global_var gets stored , i.e. which part of memory ?


    The C standard doesn't say. It's stored in different locations by
    different implementations of C. If you mention a particular
    implementation of C, and ask where global variables are stored, that's a
    question that can be answered - but this isn't the right newsgroup for
    such a question. You should look for a forum devoted to the particular
    implementation of C that you're interested in. It's also entirely
    possible that you'll find the information you're in in the
    implementation's documentation.
     
    James Kuyper, Nov 28, 2007
    #12
  13. Kislay <> writes:
    > #include<stdio.h>
    > int global_var ; // The Global Variable I am talking about
    > int main()
    > {
    > return 0;
    > }
    > Thank you all for the above posts but it still doesn't clear my
    > doubt . Where does a global variable , like the one declared in the
    > program , global_var gets stored , i.e. which part of memory ?


    As others have said, there's really no answer to your question in
    terms of C; it can vary from one implementation to another.

    Why do you want to know? What use do you intend to make of the
    information?

    If you want to know the address of global-var, the answer is
    "&globalvar".

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 28, 2007
    #13
  14. Kislay

    Kislay Guest

    What I really wanted to know was where it is stored . But you guys
    tell me that it is implementation specific . Actually this was asked
    in a job interview . And as far as I know the interviewer did not
    mention any platform .
     
    Kislay, Nov 28, 2007
    #14
  15. Kislay

    Podi Guest

    On Nov 27, 11:05 pm, Kislay <> wrote:
    > What I really wanted to know was where it is stored . But you guys
    > tell me that it is implementation specific . Actually this was asked
    > in a job interview . And as far as I know the interviewer did not
    > mention any platform .


    Ah, I usually ask this question in job interviews. I have also been
    asked many times in when I was looking for a job.

    In my opinion, it is important for a systems software engineer (who
    works on embedded or device driver software) to know the "general"
    memory structure of a process image.

    In general, it is safe to say the following since it is similar on
    Unix, Linux, Windows, ...

    char *a; // uninitialized data segment
    char *b = "hello"; // b is in uninitialized data segment, b's value
    "hello" is in constant data segment
    static char c; // file scope, not visible from other files
    char d[8] = "world"; // initialized data segment, array of 8 bytes,
    initialized first 6
    // char d[800]; will cause file size of executable to be increased
    char e[8]; // uninitialized data segment
    // char e[800]; will cause process to require more memory, but same
    executable file size

    // foo is in the code segment
    void foo(char f) // f is copied onto stack
    {
    char g; // allocated from stack
    static char h; // data segment, not on stack so the value is
    "static"
    char *i = malloc(8); // heap, potential memory leak

    // It is also important to know the difference of b and d
    d[0] = 'h'; // OK
    b[0] = 'y'; // likely to get a segmentation fault
    }
     
    Podi, Nov 28, 2007
    #15
  16. Kislay said:

    > What I really wanted to know was where it is stored .


    Like the man said, it is stored at &globalvar.

    > But you guys
    > tell me that it is implementation specific . Actually this was asked
    > in a job interview . And as far as I know the interviewer did not
    > mention any platform .


    If you're asked this at an interview, be prepared to say:

    "It depends on the implementation. For example, under Microfoo C
    implementations, it's typically stored in such-and-such a place, whereas
    in Moon C on the Pizza-Box 2000, it would go in foobar memory instead."

    For extra marks, be ready to add something like "those are the most obvious
    places but, curiously, in the Enterprise Edition of Moorland C 1962, it
    would very likely be placed in bazquux memory instead, because..."

    But the specifics are indeed implementation-specific, and this isn't the
    place to dig them out. Check the documentation for the implementation(s)
    that interest you, or that you think will interest an interviewer.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Nov 28, 2007
    #16
  17. Kislay

    santosh Guest

    Kislay wrote:

    > What I really wanted to know was where it is stored .


    The memory address that C knows about is given by applying the
    address-of operator (&) to it.

    > But you guys tell me that it is implementation specific .


    Yes. The details are very much platform specific. It depends on the
    memory models supported by the processor, operating system and system
    linker as well as the exact executable file format that you are
    compiling to.

    A good place to ask such a question might be <news:comp.lang.asm.x86>
    (for an x86 tailored answer) or <news:comp.arch>.

    There are too many architectures and too many variations of them for us
    to give you a meaningful answer.

    > Actually this was asked in a job interview . And as far as I know the
    > interviewer did not mention any platform .


    If he did not mention a platform he was probably looking for a very
    general answer or an x86 specific answer.
     
    santosh, Nov 28, 2007
    #17
  18. Kislay

    Flash Gordon Guest

    Podi wrote, On 28/11/07 07:50:
    > On Nov 27, 11:05 pm, Kislay <> wrote:
    >> What I really wanted to know was where it is stored . But you guys
    >> tell me that it is implementation specific . Actually this was asked
    >> in a job interview . And as far as I know the interviewer did not
    >> mention any platform .

    >
    > Ah, I usually ask this question in job interviews. I have also been
    > asked many times in when I was looking for a job.


    If I was asked in an interview (and I've never been asked that in an
    interview) I would take it as a probable point against the interviewer.

    > In my opinion, it is important for a systems software engineer (who
    > works on embedded or device driver software) to know the "general"
    > memory structure of a process image.


    I disagree having spent a good few years doing embedded SW. You need to
    know for setting up linker scripts, but other than that you just need to
    know the lifetime and potentially some even more implementation specific
    stuff (i.e. is it in program or data space where they are physically
    separate, or will it be in RAM or ROM etc). For all this stuff you just
    read the documentation.

    > In general, it is safe to say the following since it is similar on
    > Unix, Linux, Windows, ...
    >
    > char *a; // uninitialized data segment


    If it is at file scope, then only if something initialised the
    uninitialized data segment.

    > char *b = "hello"; // b is in uninitialized data segment, b's value
    > "hello" is in constant data segment


    "hello" is not the value of b, the value of b is the address of the
    string literal.

    > static char c; // file scope, not visible from other files


    Here you did not even attempt an answer.

    > char d[8] = "world"; // initialized data segment, array of 8 bytes,
    > initialized first 6


    Incorrect, all bytes of d are initialised.

    > // char d[800]; will cause file size of executable to be increased


    I normally find comments do not affect the size of an executable. If you
    did not intend to comment it out then I still would not necessarily
    expect it to significantly affect the size of the executable. A quick
    check on one implementation showed a size change of 18 bytes.

    > char e[8]; // uninitialized data segment


    Only if the uninitialized data segment is initialized.

    > // char e[800]; will cause process to require more memory, but same
    > executable file size


    Can't see any difference to "char d[800]" here.

    > // foo is in the code segment
    > void foo(char f) // f is copied onto stack


    Or on many implementations passed in a register.

    > {
    > char g; // allocated from stack


    Or in a register.

    > static char h; // data segment, not on stack so the value is
    > "static"
    > char *i = malloc(8); // heap, potential memory leak
    >
    > // It is also important to know the difference of b and d
    > d[0] = 'h'; // OK
    > b[0] = 'y'; // likely to get a segmentation fault
    > }


    Well, your answers if given to me at an interview would count against you.
    --
    Flash Gordon
     
    Flash Gordon, Nov 28, 2007
    #18
  19. Kislay

    Podi Guest

    On Nov 28, 11:54 am, Flash Gordon <> wrote:

    > > Ah, I usually ask this question in job interviews. I have also been
    > > asked many times in when I was looking for a job.

    >
    > If I was asked in an interview (and I've never been asked that in an
    > interview) I would take it as a probable point against the interviewer.
    >

    No offense, but you will not likely to pass our interview :)

    > > In my opinion, it is important for a systems software engineer (who
    > > works on embedded or device driver software) to know the "general"
    > > memory structure of a process image.

    >
    > I disagree having spent a good few years doing embedded SW. You need to
    > know for setting up linker scripts, but other than that you just need to
    > know the lifetime and potentially some even more implementation specific
    > stuff (i.e. is it in program or data space where they are physically
    > separate, or will it be in RAM or ROM etc). For all this stuff you just
    > read the documentation.


    Not in my group. Most of the people here have 10+ years on systems SW.
    Our software runs on many many different platforms (OS/CPU). It is
    important to know the "general" concept of process image. My emphasis
    on general, which it represents majority of our supported platforms.

    >
    > > In general, it is safe to say the following since it is similar on
    > > Unix, Linux, Windows, ...

    >
    > > char *a; // uninitialized data segment

    >
    > If it is at file scope, then only if something initialised the
    > uninitialized data segment.

    a is not at file scope.
    >
    > > char *b = "hello"; // b is in uninitialized data segment, b's value
    > > "hello" is in constant data segment

    >
    > "hello" is not the value of b, the value of b is the address of the
    > string literal.
    >

    Yes, I meant to say b's value is the memory address which the "hello"
    is stored.

    > > static char c; // file scope, not visible from other files

    >
    > Here you did not even attempt an answer.
    >

    Trivial, same memory area as a. The importance is file scope.

    > > char d[8] = "world"; // initialized data segment, array of 8 bytes,
    > > initialized first 6

    >
    > Incorrect, all bytes of d are initialised.
    >
    > > // char d[800]; will cause file size of executable to be increased

    >
    > I normally find comments do not affect the size of an executable. If you
    > did not intend to comment it out then I still would not necessarily
    > expect it to significantly affect the size of the executable. A quick
    > check on one implementation showed a size change of 18 bytes.
    >

    I meant to say if [char d[8] = "world";] was replaced by [char d[800]
    = "world";], then the file size will be increased.

    > > char e[8]; // uninitialized data segment

    >
    > Only if the uninitialized data segment is initialized.
    >
    > > // char e[800]; will cause process to require more memory, but same
    > > executable file size

    >
    > Can't see any difference to "char d[800]" here.

    Dido. The importance was to note the different effect of array size of
    d and e.
    >
    > > // foo is in the code segment
    > > void foo(char f) // f is copied onto stack

    >
    > Or on many implementations passed in a register.

    Agree. However, a CPU only has a limited number of registers. It will
    most likely be on a stack after some level of function calls.
    >
    > > {
    > > char g; // allocated from stack

    >
    > Or in a register.
    >
    > > static char h; // data segment, not on stack so the value is
    > > "static"
    > > char *i = malloc(8); // heap, potential memory leak

    >
    > > // It is also important to know the difference of b and d
    > > d[0] = 'h'; // OK
    > > b[0] = 'y'; // likely to get a segmentation fault
    > > }

    >
    > Well, your answers if given to me at an interview would count against you.
    > --
    > Flash Gordon

    You have some good points.
     
    Podi, Nov 28, 2007
    #19
  20. Kislay wrote:
    > #include<stdio.h>
    > int global_var ; // The Global Variable I am talking about
    > int main()
    > {
    > return 0;
    > }
    > Thank you all for the above posts but it still doesn't clear my
    > doubt . Where does a global variable , like the one declared in the
    > program , global_var gets stored , i.e. which part of memory ?


    The answer is: in order to programme in C, you don't need to know. You
    only need to know how it behaves.

    If you're interested in computer architectures and processor memory
    models in general, then this isn;t the right place to ask.
     
    Mark McIntyre, Nov 29, 2007
    #20
    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. =?Utf-8?B?TWF1cml6aW8gUG9sZXR0bw==?=

    How compile on fly cs file when they are stored in database

    =?Utf-8?B?TWF1cml6aW8gUG9sZXR0bw==?=, Jun 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,071
    =?Utf-8?B?TWF1cml6aW8gUG9sZXR0bw==?=
    Jun 8, 2005
  2. Asfand Yar Qazi
    Replies:
    4
    Views:
    401
    Asfand Yar Qazi
    Nov 12, 2004
  3. Martin M.
    Replies:
    4
    Views:
    345
    Simon Brunning
    Dec 15, 2005
  4. Dave Rudolf
    Replies:
    1
    Views:
    304
    Kai-Uwe Bux
    May 17, 2006
  5. David A. Black
    Replies:
    2
    Views:
    228
    Tim Hunter
    Aug 19, 2004
Loading...

Share This Page