473,785 Members | 2,310 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 #1
20 9212
deepak said:
>
Hi All,

In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?
C doesn't require the compiler to use a stack. If the compiler /does/ use a
stack, what kind of stack it uses is entirely up to it. It needn't even
grow from top to bottom or bottom to top (for example, it may be
implemented as a linked list of dynamically allocated stack frames with
arbitrary addresses).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 16 '07 #2
deepak wrote:
Hi All,

In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?
If you look at gcc source, you should find how it investigates whether
there is an apparent stack, and which way it grows. Presumably, that
code is doing things outside standard C. gcc accepts the system
employed by the bootstrap compiler. A successful gcc build shows you
already that more than one compiler can share the same scheme (the one
implemented by the hardware/OS).
Jan 16 '07 #3
Tim Prince <tp*****@nospam computer.orgwro te:
deepak wrote:
In C I heard the stack grows from top to bottom and heap from bottom to
top. Is it compiler depend?
If you look at gcc source, you should find how it investigates whether
there is an apparent stack, and which way it grows. Presumably, that
code is doing things outside standard C. gcc accepts the system
employed by the bootstrap compiler. A successful gcc build shows you
already that more than one compiler can share the same scheme (the one
implemented by the hardware/OS).
And also, conversely, that one compiler can work with different schemes.

Richard
Jan 16 '07 #4
"deepak" <de*********@gm ail.comwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.com...
>
Hi All,

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.

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.

The heap may also be OS-dependent. In the old days, a program had nearly
the whole machine memory to itself, and it would start the stack and heap at
opposite ends of free memory and maximize the time until they collided.
These days when OSs are more sophisticated and there is memory management
hardware in place, I doubt it is done this way.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 16 '07 #5
In article <5o************ *************** ***@giganews.co m>,
David T. Ashley <dt*@e3ft.comwr ote:
>The direction the stack grows is going to be architecture-dependent.
True.
>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 necessarily true. On some processors, the direction might just
be a matter of convention, and one OS might support different ABI
(Application Binary Interfaces) that use different conventions.
There are machines for which the "preferred" direction is controlled
by a CPU status register, alterable by the kernel.

Some machines do not have "PUSH" or "CALL" instructions with
built-in directions: they might instead just have "autoincrem ent"
and "autodecrem ent" addressing modes, and a PUSH might be
something like MOV.L 3248(R11), -(R7) where R7 as stack pointer
is just an ABI convention and where (R7)+ could be used by an ABI
instead to grow the stack in the other direction.

C compilers for such machines follow the OS ABI conventions, rather
than being forced into one direction or another according to some
hardware architecture limitation.

>The heap may also be OS-dependent. In the old days, a program had nearly
the whole machine memory to itself, and it would start the stack and heap at
opposite ends of free memory and maximize the time until they collided.
These days when OSs are more sophisticated and there is memory management
hardware in place, I doubt it is done this way.
It is still often done more or less that way, but these days you
have to worry about where the shared libraries or DLLs are going to
pop up in your address space, and have to worry about where any shared
memory segments will appear. On some systems, large heap allocations might
get turned into shared memory segment allocations (making it easy to
release the entire block back to the OS when the process free()'s it...
something that has been traditionally hard to do.)
--
Programming is what happens while you're busy making other plans.
Jan 16 '07 #6
"David T. Ashley" <dt*@e3ft.comwr ites:
"deepak" <de*********@gm ail.comwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.com...
>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.
[...]

Even in "the early days of the PC when 640K was the rule", there were
plenty of C implementations for systems other than PCs.

--
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 16 '07 #7
deepak wrote:
In C I heard the stack grows from top to bottom and heap from bottom to
top.
Is it compiler depend?
It depends on the escutcheon grommets.

--
Peter

Jan 16 '07 #8
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.

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.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 16 '07 #9
>In C I heard the stack grows from top to bottom and heap from bottom to
>top.
C doesn't define anything called a "top" or "bottom" of memory.
Typically the "top" is the memory address closest to the top of the
page on a memory diagram written on a piece of paper. That usually
has little to do with the machine involved and more to do with the
person who drew the diagram.
>Is it compiler depend?
C doesn't define anything called a "stack" or "heap", either, and
there's no guarantee that these are mutually exclusive.
Jan 17 '07 #10

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

Similar topics

11
2314
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
3045
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
6095
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
2553
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
2889
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
6589
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
9647
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
9485
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
10356
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
10161
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...
1
10098
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,...
1
7506
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
6743
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
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
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.