where global variables created in memory? STACK or HEAP

Discussion in 'C Programming' started by S. A. Hussain, Aug 12, 2003.

  1. Where Global variables created in STACK or HEAP in C/C++?

    ve##tolimits[at]yahoo[dot]com, delete ##
    S. A. Hussain, Aug 12, 2003
    #1
    1. Advertising

  2. S. A. Hussain

    Derk Gwen Guest

    (S. A. Hussain) wrote:
    # Where Global variables created in STACK or HEAP in C/C++?

    Globals exist in a memory area that exists from before the first reference
    in a program until after the last reference. Beyond that should be irrelevant
    to the correct functionning of practically every program. The actual area is
    implementation dependent: it could be on the heap accessed through link segments,
    or at the bottom of the stack. Often it's located in another area which is
    neither heap nor stack.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I have no idea what you just said.
    I get that alot.
    Derk Gwen, Aug 12, 2003
    #2
    1. Advertising

  3. S. A. Hussain

    Dan Pop Guest

    In <> (S. A. Hussain) writes:

    >Where Global variables created in STACK or HEAP in C/C++?


    Typically, neither on stack nor on heap. Due to their nature, they are
    typically allocated in a data segment whose size remains constant
    for the whole program duration. Or in more such segments. Data
    structures like stacks and heaps are typically used for variables that
    get created and destroyed during the program execution.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 12, 2003
    #3
  4. S. A. Hussain

    Dan Pop Guest

    In <Xns93D5732BF79B8lkj562ghjgk1k245lbvj@130.133.1.4> "Mark A. Odell" <> writes:

    >"Mark A. Odell" <> wrote in
    >news:Xns93D572D264EE4lkj562ghjgk1k245lbvj@130.133.1.4:
    >
    >> (S. A. Hussain) wrote in
    >> news::
    >>
    >>> Where Global variables created in STACK or HEAP in C/C++?

    >>
    >> It's up to the compiler. Stack and Heap are implementation constructs.

    >
    >Having said that, "Global" variables, those that are at file scope and can
    >be externed for use in other translation units are typcally not stored in
    >a stack-like region but are placed in what some implementations call .bss
    >or .data segments.


    Or bss *and* data segments. The data segment is used for initialised
    variables and the bss segment for the variables that get initialised by
    default to all zeroes (on most implementations, all zeroes is good enough
    for pointers and floating point variables, too). Which basically means
    that an image of the data segment must be present in the executable binary
    but all the information needed about the bss segment is its size and
    relative address (the OS will typically initialise it to all zeroes, so
    the startup code need not bother).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 12, 2003
    #4
  5. (Dan Pop) wrote in news:bhb4jg$bge$:

    >>>> Where Global variables created in STACK or HEAP in C/C++?
    >>>
    >>> It's up to the compiler. Stack and Heap are implementation constructs.

    >>
    >>Having said that, "Global" variables, those that are at file scope and
    >>can be externed for use in other translation units are typcally not
    >>stored in a stack-like region but are placed in what some
    >>implementations call .bss or .data segments.

    >
    > Or bss *and* data segments. The data segment is used for initialised
    > variables and the bss segment for the variables that get initialised by
    > default to all zeroes (on most implementations, all zeroes is good
    > enough for pointers and floating point variables, too).


    No kidding. My point was, a given variable goes into to one or the other,
    not that all "globals" go into one or the other. I'll watch my language
    with better precision next time.

    --
    - Mark ->
    --
    Mark A. Odell, Aug 12, 2003
    #5
  6. S. A. Hussain

    jemma Guest

    On Tue, 12 Aug 2003 21:44:12 GMT, CBFalconer <>
    wrote:

    >Neither Global, STACK nor HEAP are mentioned in the C standard,
    >and are thus off-topic here.


    Really? The concepts of 'global', 'stack', and 'heap' are generic
    stuff of computer programming. Are you suggesting that all discussions
    on c.l.c be constrained to the actual vocabulary of the standards? Are
    we allowed to use terms and phrases in the 'Rationales' and
    appendices?

    While 'global variable' may be less precise than 'a variable of static
    duration with external linkage' in specific, limited circumstances,
    this doesn't appear to be one of them.

    Doesn't the last-in/first-out semantics of the scoping of local
    variables (sorry, I mean 'automatic' variables) within nested blocks
    describe a stack?

    Aren't malloc() and free() the primary allocation and deallocation
    routines for a data structure commonly known as a 'heap'?

    >I suspect you want a news group that deals with your particular OS
    >and/or compiler.


    Based solely on the excellent answers in reply to the OP's question
    (most posted hours before your less-than-helpful one), maybe not. I
    think you're being way too picky. I knew /exactly/ what the OP meant,
    so did a number of other responders, and, I suspect, so did you.

    --Jemma
    jemma, Aug 13, 2003
    #6
  7. S. A. Hussain

    Chris Dollin Guest

    jemma wrote:

    > Doesn't the last-in/first-out semantics of the scoping of local
    > variables (sorry, I mean 'automatic' variables) within nested blocks
    > describe a stack?


    No. They describe something for which a stack is a natural implementation,
    but it isn't *required* to be implemented that way. What's more,
    within a particular function, nested blocks typically *don't* use
    a stack; variables are allocated fixed positions in the function's
    local-variable-storage-area-but-don't-call-me-a-frame; there's no
    pushing and popping at run-time.

    > Aren't malloc() and free() the primary allocation and deallocation
    > routines for a data structure commonly known as a 'heap'?


    No. You *can* implement them that way, using one of the things called
    a "heap" (but not the other one, which is the one I'd think of as
    "a data structure"), but that's not a requirement.

    >>I suspect you want a news group that deals with your particular OS
    >>and/or compiler.

    >
    > Based solely on the excellent answers in reply to the OP's question
    > (most posted hours before your less-than-helpful one), maybe not. I
    > think you're being way too picky. I knew /exactly/ what the OP meant,
    > so did a number of other responders, and, I suspect, so did you.


    And yet it's still worth trying to keep the terminology clear and
    Standard-oriented, because there are times when thinking in terms
    of "stack" and "heap" [etc] is exactly what the confusion is.

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Aug 14, 2003
    #7
  8. S. A. Hussain

    Randy Howard Guest

    In article <>,
    says...
    > Where Global variables created in STACK or HEAP in C/C++?


    You never know, probably neither. Move on.

    >
    > ve##tolimits[at]yahoo[dot]com, delete ##
    >


    You do realize that your From: line in the header is in
    the clear? All this ## stuff isn't going to save you
    from the address reaper now.

    Welcome to spam.
    Randy Howard, Aug 15, 2003
    #8
    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. Gurikar

    heap vs stack memory

    Gurikar, May 19, 2005, in forum: C++
    Replies:
    4
    Views:
    11,894
    Gurikar
    May 19, 2005
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,890
    Bryan Bullard
    Jul 11, 2003
  3. Pieter Claassen

    When to use memory on heap or stack

    Pieter Claassen, Aug 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    416
    Malcolm
    Aug 17, 2004
  4. Amit
    Replies:
    10
    Views:
    866
    Flash Gordon
    Oct 18, 2005
  5. Replies:
    9
    Views:
    432
    peter koch
    May 26, 2007
Loading...

Share This Page