Stack Variables

A

Andre

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
 
C

Chris Dollin

Andre said:
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.]
 
K

Kevin Easton

Andre said:
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.
 
L

Lew Pitcher

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')
 
A

Andre

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
 
J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
M

Mark A. Odell

Andre said:
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.
 
D

Dan Pop

In said:
It depends on your implementation.

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

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top