By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Stack Variables

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #2

P: n/a
Andre <fo********@hotmail.com> 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.

Nov 13 '05 #3

P: n/a
> 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

Nov 13 '05 #4

P: n/a
Andre <fo********@hotmail.com> 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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #5

P: n/a
Andre <fo********@hotmail.com> wrote in news:3F**************@hotmail.com:
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 ->
--
Nov 13 '05 #6

P: n/a
Thanks Kevin, that answered my question :)

-Andre

Kevin Easton wrote:
Andre <fo********@hotmail.com> 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.


Nov 13 '05 #7

P: n/a
On Thu, 03 Jul 2003 21:49:45 +1000, Andre <fo********@hotmail.com> 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')
Nov 13 '05 #8

P: n/a
In <be**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Andre <fo********@hotmail.com> 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: Da*****@ifh.de
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.