473,378 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Stack Variables

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
8 9550
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
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
> 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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.