473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How the stack grows


Hi All,

In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?

Jan 16 '07
20 9214
David T. Ashley wrote:
The direction the stack grows is going to be architecture-dependent. All
machines have a certain direction the stack pointer goes on a subroutine
call and instructions like PUSH, and typically a 'C' compiler will build
stack frames starting with this convention.
Not /all/ machines, the ARM being the obvious example.

(The subroutine call instruction doesn't use any stack at all;
the return address is saved in R14 and restoring that value
to the PC (which happens to be R15) accomplishes a return.
If you want to stack things, the LDR/STR instructions have
enough addressing modes that you can stack either way up; more
crucially, the LDM/STM instructions, which save/restore
multiple registers to sequential locations, have all four of
the increment vs decrement and before vs after variants, so
you can stack either way up with those too. Which ones you
use depend on the environment the code runs in, not some
predetermined hardware choice. As for interrupts, those are
initially handled using shadow registers, not a stack -- so
again, any stack conventions are just that: conventions.

I seem to recall that the C implementation used threaded
stack chunks, not a single contiguous space, so local
variables in different functions [1] could have wildly
different addresses with no necessary consistency in
"grow up" or "grow down", but it's been a long time since
I deserted RISC OS for Linux [2].)

[1] one of which called the other.

[2] I'll just go and have a little cry.

--
Chris "electric hedgehog" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jan 17 '07 #11
deepak wrote:
In C, I heard that the stack grows from top to bottom
and heap from bottom to top.
Is it compiler depend?
A stack always grows from bottom to top
but neither computers or compilers know up from down.
They don't need to
but programmers sometimes find the distinction convenient.

In the typical implementation,
low memory addresses are associated with the top of memory
and high memory addresses are associated with the bottom of memory.

0000 0000 (top of memory)
0000 0001
Jan 18 '07 #12
"E. Robert Tisdale" <ed***@netwood. netwrites:
deepak wrote:
In C, I heard that the stack grows from top to bottom
and heap from bottom to top.
Is it compiler depend?

A stack always grows from bottom to top
but neither computers or compilers know up from down.
They don't need to
but programmers sometimes find the distinction convenient.
If you define "top" and "bottom" that way.
In the typical implementation,
low memory addresses are associated with the top of memory
and high memory addresses are associated with the bottom of memory.
[...]

The stack, for systems that have such a thing, can grow in either
direction. For well-written C code, it doesn't matter.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 18 '07 #13

E. Robert Tisdale wrote:
deepak wrote:
In C, I heard that the stack grows from top to bottom
and heap from bottom to top.
Is it compiler depend?

A stack always grows from bottom to top
but neither computers or compilers know up from down.
They don't need to
but programmers sometimes find the distinction convenient.

In the typical implementation,
low memory addresses are associated with the top of memory
and high memory addresses are associated with the bottom of memory.
Haven't you got this backwards from how it's usually considered?

Jan 18 '07 #14

santosh wrote:
E. Robert Tisdale wrote:
deepak wrote:
In C, I heard that the stack grows from top to bottom
and heap from bottom to top.
Is it compiler depend?
A stack always grows from bottom to top
but neither computers or compilers know up from down.
They don't need to
but programmers sometimes find the distinction convenient.

In the typical implementation,
low memory addresses are associated with the top of memory
and high memory addresses are associated with the bottom of memory.

Haven't you got this backwards from how it's usually considered?
Not necessarily. I've seen a number of implementations that grow from
high to low memory.

If you're implementing a stack as a simple array and updating your
stack pointer via ++ and --, going from high to low is dead easy:

#define N ...
static value_t stack[N];
static size_t sp = 0;

void push(value_t val)
{
if (sp 0)
stack[--sp] = val;
else
// overflow
}

void pop(value_t *val)
{
if (sp < N)
*val = stack[sp++];
else
// underflow
}

Granted, going from low to high isn't that much more complicated, but
it isn't quite this straightforward .

Jan 18 '07 #15

John Bode wrote:
santosh wrote:
E. Robert Tisdale wrote:
deepak wrote:
>
In C, I heard that the stack grows from top to bottom
and heap from bottom to top.
Is it compiler depend?
>
A stack always grows from bottom to top
but neither computers or compilers know up from down.
They don't need to
but programmers sometimes find the distinction convenient.
>
In the typical implementation,
low memory addresses are associated with the top of memory
and high memory addresses are associated with the bottom of memory.
Haven't you got this backwards from how it's usually considered?

Not necessarily. I've seen a number of implementations that grow from
high to low memory.

If you're implementing a stack as a simple array and updating your
stack pointer via ++ and --, going from high to low is dead easy:

#define N ...
static value_t stack[N];
static size_t sp = 0;
Of course, that line *should* read

static size_t sp = N;

Serves me right for posting before I'm really awake.

Jan 18 '07 #16

deepak schrieb:
Hi All,

In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?
While the stack growing direction is mostly based on machine dependent
stack treatment (e.g. Intel cpus have PUSH, POP etc. using a cpu
register
as stack pointer that is /decremented/ by a PUSH), heap management
is purely software oriented and thus either OS-dependent or not even
that
(i.e. compiler dependent, where the compiler might base its decision on
OS
and architecture). Heap management methods within your own projects
might even be a configurable option.

Jan 18 '07 #17
santosh wrote:
E. Robert Tisdale wrote:
>deepak wrote:
>>In C, I heard that the stack grows from top to bottom
and heap from bottom to top. Is it compiler depend?

A stack always grows from bottom to top but neither computers or
compilers know up from down. They don't need to but programmers
sometimes find the distinction convenient.

In the typical implementation, low memory addresses are
associated with the top of memory and high memory addresses are
associated with the bottom of memory.

Haven't you got this backwards from how it's usually considered?
Tisdale is normally wrong. Best ignored.

--
Sending unsolicited commercial e-mail to
cbfalconer at maineline dot net
incurs a fee of 500 USD per message, and
acknowledges the legality of this contract.
Jan 18 '07 #18
On 16 Jan 2007 22:55:33 GMT, ri*****@cogsci. ed.ac.uk (Richard Tobin)
wrote:
In article <5o************ *************** ***@giganews.co m>,
David T. Ashley <dt*@e3ft.comwr ote:
In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?
In the early days of the PC when 640K was the rule, this may have been the
case. I'm not sure it is the case today.

When talking about C, a more natural computer to consider is the PDP-11.
PCs were latecomers to the C party.

And interestingly, the PDP-11 is an example of a machine where stacks
can be easily used in either direction. Rather than a "push"
instruction, it has auto-increment and auto-decrement modes for
register access.
Stacks in general almost, but the stack used for 'hardware' (ISA)
calls -- and as noted elsethread, in block-frame languages including C
it is usually convenient to put locals and params/args together with
calls -- JSR and RTS use R6=(U?)SP growing downward. It is possible to
cobble together your own calling sequence not using -(R6), but
significantly more tedious for IME&O zero benefit.

And even for other (non-call) stacks, downward push *--r and pop *r++
allow you to peek at the top (single) element more cheaply than the
upward counterparts. FW(L?)IW.
Even on such a machine, there are typically good reasons to use a
particular direction for the call stack. One is compatibility
with other languages on the same system, and another is that the
hardware may assume a particular stack direction when generating
interrupts, though I think it's rare for hardware interrupts to use
the user stack these days.
-11 interrupts/traps and RTI/RTT also use R6=(K?)SP downward, but on
models with memory management it indeed is the K (kernel) one, and at
least the most important -11 OS on which C was implemented (wink,
wink, nudge, nudge <G>) required such models.

I don't recall if there was a C implementation for RT-11 non-XM; there
almost certainly wasn't a conforming one, if in fact there was still
any significant development on or targetting that system by '89.

- David.Thompson1 at worldnet.att.ne t
Jan 29 '07 #19
On Mon, 29 Jan 2007 00:03:33 GMT,
Dave Thompson <da************ *@worldnet.att. netwrote:
Stacks in general almost, but the stack used for 'hardware'
(ISA) calls -- and as noted elsethread, in block-frame
languages including C it is usually convenient to put locals
and params/args together with calls -- JSR and RTS use
R6=(U?)SP growing downward. It is possible to cobble together
your own calling sequence not using -(R6), but significantly
more tedious for IME&O zero benefit.

And even for other (non-call) stacks, downward push *--r and
pop *r++ allow you to peek at the top (single) element more
cheaply than the upward counterparts. FW(L?)IW.
Hmmm, isn't a downward stack implemented with:

push: *--r = rvalue;
pop: return *r++;

more or less equivalent with an upward growing stack which uses:

push: *++r = rvalue;
pop: return *r--;

or am I missing something fundamental about downward growing stacks?

Feb 20 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2316
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting most/all of it on the stack? Does it matter? Any considerations I might take into account when deciding how to allocate the many data structures? By the way, the system will consist of a handful of very large data structures (primarily matrices...
21
3047
by: puzzlecracker | last post by:
Guys - Is it possible to find which way system stack grows? Perhaps, I am not precise enough: When a function is called, the return address of (callee) function is put on the stack. Thus, the question is the direction where that stack heading (address increasing or decreasing). How would you implement this in cpp? any suggestion.
10
8349
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But what about the global space? Is that the same as the heap space or is that still a form of special stack? Because once I've seen someone declare a 1 megabyte char array in global space like this: char s;
41
6096
by: Nitin Bhardwaj | last post by:
Hi all, I wanted to know whether the stack in a C program is growing upwards or downwards.So I wrote a little code to see that.Please guide me as to whether this code is correct in telling this ? #include <stdio.h> int main(void) {
6
2555
by: Adam Warner | last post by:
Hi all, Is this a (C99) portable way to detect whether the C stack grows upwards (1) or downwards (-1)? #include <stdio.h> int stack_direction=0; void detect_stack_direction(void * stack_start) {
26
2891
by: bahadir.balban | last post by:
Hi, When you define varibles in the middle of your function call (C99), such as: if(i == 5) { int x = 5; int z = 2; }
18
7353
by: junky_fellow | last post by:
Hi all, Is there any way by which we mat determine the direction of stack growth (from higher address to lower address Or from lower address to higher address) ? I know this question is implementation specific and this may not be the correct place to ask this. But any hints would help me a lot.
24
6592
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
5
4700
by: Joel | last post by:
I would like to learn if there is any difference between the stack that MSIL uses for each method for executing instructions and the stack that it uses for storing Value types. Are they the same? Also the processor has its own stack pointer right,.. so are these stacks related to the processor's stack? or are they virtually implemented by the CLR ?
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.