473,796 Members | 2,590 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
On Tue, 20 Feb 2007 01:24:45 +0200, Giorgos Keramidas
<ke******@ceid. upatras.grwrote :
(sorry for the delay, I overlooked this in my new drafts folder)
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++;
Yes.
more or less equivalent with an upward growing stack which uses:

push: *++r = rvalue;
pop: return *r--;
Those aren't addressing modes on PDP-11. You can use
MOV psh, (r)+ ; dst mode 2r ;; *r++ = psh
MOV -(r), pop ; src mode 4r ;; pop = *--r
; or something else like ADD -(r), dst
but then 'peeking' at TOS element is -2(r) mode 6r which requires an
extra word not (r) mode 1r which does not and is sufficient on
downward, hence my 'more cheaply'.
or am I missing something fundamental about downward growing stacks?
I wouldn't call it fundamental, but the -11 architecture does have a
slight bias favoring downward as I described, in addition to its
(forced) use for the hardware functions (JSR, intr/trap) I described.

- David.Thompson1 at worldnet.att.ne t
Mar 17 '07 #21

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
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...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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.