Stack Variables

Discussion in 'C Programming' started by Andre, Jul 3, 2003.

  1. Andre

    Andre Guest

    Hi,

    If I say:

    int i = 5;

    Does 'i' get stored on the stack? If yes, where *is* the stack? On the
    heap? What manages the stack and how does it get created? Thanks

    -Andre
     
    Andre, Jul 3, 2003
    #1
    1. Advertising

  2. Andre

    Chris Dollin Guest

    Andre wrote:

    > Hi,
    >
    > If I say:
    >
    > int i = 5;
    >
    > Does 'i' get stored on the stack? If yes, where *is* the stack? On the
    > heap? What manages the stack and how does it get created? Thanks


    What stack? What heap?

    "It depends on your implementation".

    However, if you really meant a different question, a variable allocated
    outside a function will exist for the duration of the program, but
    one allocated inside a function, if not declared `static`, will cease
    to exist when the function invocation that created it exits.

    By a strange coincidence, this allows such variables to be allocated
    on a stack, if the implementation finds it convenient. But of these
    matters we do not speak freely here.

    [Nor do we freely do homework. We even charge ourselves for that.]

    --
    Chris "firewalls are OK. broken firewalls ARE NOT OK AT ALL." 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, Jul 3, 2003
    #2
    1. Advertising

  3. Andre

    Kevin Easton Guest

    Andre <> wrote:
    > Hi,
    >
    > If I say:
    >
    > int i = 5;
    >
    > Does 'i' get stored on the stack?


    There isn't necessarily a "the stack". On typical implementations, the
    answer to your question is "it depends" - if the line was at file scope,
    it would usually be stored in an initialised data segment, but if it's
    at block scope, it would be stored on an argument stack (or something
    that behaves like an argument stack, from the point of view of a C
    program - that is, each recusive invocation of a function creates a new
    instance of the object, disjoint from the existing instances of that
    object, and as the functions return, the objects are deallocated in
    reverse order).

    None of this matters for writing portable C programs though - all that
    matters, from the C point of view, is the lifetime of the object. There
    are three possible object lifetimes in C - static storage duration,
    which applies to file-scope objects and block-scope objects declared
    with the static keyword; automatic storage duration, which applies to
    all other objects declared at block scope; and dynamic storage duration,
    which applies to objects created with malloc(), calloc() or realloc().

    Static storage duration means the object lives for as long as the
    program does. Automatic storage duration means the object is created
    when the program begins executing the block it's declared in, and ceases
    existing when the end of the block is reached. Dynamic storage duration
    means the object is created at the time of themalloc()/calloc()/realloc()
    call, and ceases to exist at the time of the corresponding free().

    > If yes, where *is* the stack? On the heap?


    Probably not, because a stack needs to be in contiguous memory.
    Typically this is solved by having the heap grow upwards from the bottom
    of memory, and the stack grow downwards from the top.

    > What manages the stack and how does it get created? Thanks


    Code your compiler creates as part of the process of compiling your
    program - sometimes with help from special CPU instructions, sometimes
    not.

    - Kevin.
     
    Kevin Easton, Jul 3, 2003
    #3
  4. Andre

    Lew Pitcher Guest

    On Thu, 03 Jul 2003 21:49:45 +1000, Andre <> wrote:

    >Hi,
    >
    >If I say:
    >
    >int i = 5;
    >
    >Does 'i' get stored on the stack?


    The C standard does not say. Your compiler /may/ implement automatic variables
    on a stack, but it is under no obligation from the C standard to do so.

    > If yes, where *is* the stack?


    The C standard does not say. Your compiler is free to implement it's stack in
    any manner suitable to your OS.


    > On the
    >heap?


    The C standard does not say. Your compiler is free to implement it's stack in
    any manner suitable to your OS.

    > What manages the stack and how does it get created? Thanks


    The C standard does not say. Your compiler is free to implement stack management
    in any manner suitable to your OS.


    --
    Lew Pitcher
    IT Consultant, Enterprise Technology Solutions
    Toronto Dominion Bank Financial Group

    (Opinions expressed are my own, not my employers')
     
    Lew Pitcher, Jul 3, 2003
    #4
  5. Andre

    Andre Guest

    > By a strange coincidence, this allows such variables to be allocated
    > on a stack,


    Exactly... so how is this stack created or managed? By the C compiler?

    I have a x86 (pentium 4).

    Thanks

    -Andre
     
    Andre, Jul 3, 2003
    #5
  6. Andre <> scribbled the following:
    >> By a strange coincidence, this allows such variables to be allocated
    >> on a stack,


    > Exactly... so how is this stack created or managed? By the C compiler?


    > I have a x86 (pentium 4).


    Discussion of how the C compiler works is outside the scope of
    comp.lang.c. You'll have to ask in a newsgroup dedicated to your own
    specific compiler.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "I wish someone we knew would die so we could leave them flowers."
    - A 6-year-old girl, upon seeing flowers in a cemetery
     
    Joona I Palaste, Jul 3, 2003
    #6
  7. Andre <> wrote in news::

    >> By a strange coincidence, this allows such variables to be allocated
    >> on a stack,

    >
    > Exactly... so how is this stack created or managed? By the C compiler?


    On the 8051 (an 8-bit microcontroller), many of the C compilers don't use
    any stack for local automatics. Of course you lose reentrancy and these
    compilers are not ANSI compliant in this mode but the point is "what
    stack"?

    > I have a x86 (pentium 4).


    You seek an x86 newsgroup then, don't you? You question is not about C but
    about an implementation of it which is off-topic here.

    --
    - Mark ->
    --
     
    Mark A. Odell, Jul 3, 2003
    #7
  8. Andre

    Andre Guest

    Thanks Kevin, that answered my question :)

    -Andre

    Kevin Easton wrote:
    > Andre <> wrote:
    >
    >>Hi,
    >>
    >>If I say:
    >>
    >>int i = 5;
    >>
    >>Does 'i' get stored on the stack?

    >
    >
    > There isn't necessarily a "the stack". On typical implementations, the
    > answer to your question is "it depends" - if the line was at file scope,
    > it would usually be stored in an initialised data segment, but if it's
    > at block scope, it would be stored on an argument stack (or something
    > that behaves like an argument stack, from the point of view of a C
    > program - that is, each recusive invocation of a function creates a new
    > instance of the object, disjoint from the existing instances of that
    > object, and as the functions return, the objects are deallocated in
    > reverse order).
    >
    > None of this matters for writing portable C programs though - all that
    > matters, from the C point of view, is the lifetime of the object. There
    > are three possible object lifetimes in C - static storage duration,
    > which applies to file-scope objects and block-scope objects declared
    > with the static keyword; automatic storage duration, which applies to
    > all other objects declared at block scope; and dynamic storage duration,
    > which applies to objects created with malloc(), calloc() or realloc().
    >
    > Static storage duration means the object lives for as long as the
    > program does. Automatic storage duration means the object is created
    > when the program begins executing the block it's declared in, and ceases
    > existing when the end of the block is reached. Dynamic storage duration
    > means the object is created at the time of themalloc()/calloc()/realloc()
    > call, and ceases to exist at the time of the corresponding free().
    >
    >
    >>If yes, where *is* the stack? On the heap?

    >
    >
    > Probably not, because a stack needs to be in contiguous memory.
    > Typically this is solved by having the heap grow upwards from the bottom
    > of memory, and the stack grow downwards from the top.
    >
    >
    >>What manages the stack and how does it get created? Thanks

    >
    >
    > Code your compiler creates as part of the process of compiling your
    > program - sometimes with help from special CPU instructions, sometimes
    > not.
    >
    > - Kevin.
    >
     
    Andre, Jul 3, 2003
    #8
  9. Andre

    Dan Pop Guest

    In <be15kc$gna$> Joona I Palaste <> writes:

    >Andre <> scribbled the following:
    >> Hi,

    >
    >> If I say:

    >
    >> int i = 5;

    >
    >> Does 'i' get stored on the stack? If yes, where *is* the stack? On the
    >> heap? What manages the stack and how does it get created? Thanks

    >
    >It depends on your implementation.


    It also depends on where the definition in question is actually located:
    inside or outside a function.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 3, 2003
    #9
    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. Surinder Singh
    Replies:
    1
    Views:
    1,214
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,104
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,320
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    228
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    264
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page