473,396 Members | 1,892 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,396 software developers and data experts.

where does system stack grow in cpp?

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.

Oct 6 '05 #1
21 2999
puzzlecracker wrote:
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.


Just call a function that takes a parameter, take the address of the
parameter, then call another function that takes a parameter and take the
address of that parameter. Print out the addresses and you'll see in which
direction they are heading.

DW
Oct 6 '05 #2
David White wrote:
puzzlecracker wrote:
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.
Just call a function that takes a parameter, take the address of the
parameter, then call another function


from within the first function
that takes a parameter and take
the address of that parameter. Print out the addresses and you'll see
in which direction they are heading.

DW

Oct 6 '05 #3

David White wrote:
puzzlecracker wrote:
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.


Just call a function that takes a parameter, take the address of the
parameter, then call another function that takes a parameter and take the
address of that parameter. Print out the addresses and you'll see in which
direction they are heading.

DW


That will NOT work.
you can pass either by value or by reference (by pointer is the same in
the given context).

If pass by value - taking it address will return the address in the
current frame even if you pass it to function - and the value is copied
to the function's frame. Likewise, passing it by referenfce will not
show the difference.

Oct 6 '05 #4

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Guys -

Is it possible to find which way system stack grows?
Not with standard C++. The language makes no requirement
that a 'system stack' or 'stack' exists at all.
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?


Can't be done. Perhaps you could find out by consulting
documentation of your platform.

-Mike
Oct 6 '05 #5
* puzzlecracker:
* David White:

Just call a function that takes a parameter, take the address of the
parameter, then call another function that takes a parameter and take the
address of that parameter. Print out the addresses and you'll see in which
direction they are heading.


That will NOT work.


Works fine in practice. Instead of a parameter you can use a local variable.
It's the same. Of course there are numerous theoretical reasons why it might
not work, such as (1) you declared the parameters/variables as 'register', and
your code won't compile, or (2) you declared the functions as 'fastcall' or
some such using compiler-specific directives, or (3) you're using a C++
implementation that allocates everything dynamically -- I think there's an
interpreter somewhere (quincy?) that does that... Anyway, when you know that
you don't know much about some aspect, it's a good idea to try out things.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 6 '05 #6
puzzlecracker wrote:
David White wrote:
Just call a function that takes a parameter, take the address of the
parameter, then call another function that takes a parameter and
take the address of that parameter. Print out the addresses and
you'll see in which direction they are heading.

DW
That will NOT work.


I think it will. Try it.
you can pass either by value
I meant by value.
or by reference (by pointer is the same
in the given context).

If pass by value - taking it address will return the address in the
current frame even if you pass it to function - and the value is
copied to the function's frame.


And what I suggested was to take the address of the parameter _as received
by the function_, i.e., after a copy of the argument is pushed onto the
stack. Or use local variables as APS suggested. Two or more nested calls
will reveal the direction in which the stack is growing.

DW
Oct 6 '05 #7

Alf P. Steinbach wrote:
* puzzlecracker:
* David White:

Just call a function that takes a parameter, take the address of the
parameter, then call another function that takes a parameter and take the
address of that parameter. Print out the addresses and you'll see in which
direction they are heading.


That will NOT work.


Works fine in practice. Instead of a parameter you can use a local variable.
It's the same. Of course there are numerous theoretical reasons why it might
not work, such as (1) you declared the parameters/variables as 'register', and
your code won't compile, or (2) you declared the functions as 'fastcall' or
some such using compiler-specific directives, or (3) you're using a C++
implementation that allocates everything dynamically -- I think there's an
interpreter somewhere (quincy?) that does that... Anyway, when you know that
you don't know much about some aspect, it's a good idea to try out things.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

everyone is macking such radical claims:

I am thinking to determing this we can do something along these line
(CODE IS LEGAL IN standard c++ - which isn't concerned with stacks
grow but with its determination)

include<iostream>

int * function(){ int b; return &b; }

int main (){

int a;
long diff=static_cast<long>(&a -function());
if(diff<=0)
std::cout<<"grows up\n";
else
std::cout<<"grows down\n";

}

this will work given that above function() will NOT be inlined.

By the way, is there a directive to forcefully prohibit a compiler
from inlining?

Oct 6 '05 #8
puzzlecracker wrote:
everyone is macking such radical claims:
Radical?
I am thinking to determing this we can do something along these line
(CODE IS LEGAL IN standard c++ - which isn't concerned with stacks
grow but with its determination)

include<iostream>

int * function(){ int b; return &b; }

int main (){

int a;
long diff=static_cast<long>(&a -function());
if(diff<=0)
std::cout<<"grows up\n";
else
std::cout<<"grows down\n";

}

this will work given that above function() will NOT be inlined.
This is just another form of what was suggested in the previous replies.
By the way, is there a directive to forcefully prohibit a compiler
from inlining?


Not in the language standard. Check your compiler's documentation.

DW
Oct 6 '05 #9
This is dependent on the processor architecture. The code generator for
your C++ compiler would be using the stack organization that is
supported by the compiler.

For example, on the 68000 processor a push is a predecrement on the
stack pointer while the op is a post increment. Thus the stack grows by
decreasing the address. The following article describes this:

http://www.eventhelix.com/RealtimeMa...ranslation.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

Oct 6 '05 #10
Alf P. Steinbach wrote:
Of course there are numerous theoretical reasons why it might not work,
such as (1) you declared the parameters/variables as 'register', and your
code won't compile, or (2) you declared the functions as 'fastcall' or
some such using compiler-specific directives, or (3) you're using a C++
implementation that allocates everything dynamically
or (4) the compiler puts the parameters in registers by default or (5) the
function gets inlined, ...
Anyway, when you know that you don't know much about some aspect, it's a
good idea to try out things.


It's an even better idea to consult the manual of the platform/CPU if you
want to know how it works. There is a lot of code out there that is based
on false assumptions because people tried things out instead of RingTFM,
especially in the C++ area. And then, when they want to port it, they think
the compiler is broken, because the code won't work anymore.

Oct 6 '05 #11
EventHelix.com wrote:
This is dependent on the processor architecture. The code generator for
your C++ compiler would be using the stack organization that is
supported by the compiler.

For example, on the 68000 processor a push is a predecrement on the
stack pointer while the op is a post increment. Thus the stack grows by
decreasing the address. The following article describes this:


Actually, I think that all architectures have stack growing down today.

Mirek
Oct 6 '05 #12

Mirek Fidler wrote:
EventHelix.com wrote:
This is dependent on the processor architecture. The code generator for
your C++ compiler would be using the stack organization that is
supported by the compiler.

For example, on the 68000 processor a push is a predecrement on the
stack pointer while the op is a post increment. Thus the stack grows by
decreasing the address. The following article describes this:


Actually, I think that all architectures have stack growing down today.

Mirek


MYy question is how to determine it programatically?

Oct 6 '05 #13
puzzlecracker wrote:

Mirek Fidler wrote:

[snip]
Actually, I think that all architectures have stack growing down today.

Mirek


MYy question is how to determine it programatically?


Well, if M. Fidler is correct, then the following code will do:

#include <iostream>

int main ( void ) {
std::cout << "stack grows down.\n";
}
Best

Kai-Uwe Bux
Oct 6 '05 #14
puzzlecracker wrote:
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? cat main.cpp #include <iostream>

unsigned int factorial(unsigned int n) {
std::cerr << n << ": " << &n << std::endl;
return (1 < n)? n*factorial(n - 1): 1;
}

int main(int argc, char* argv[]) {
std::cout << "5! = " << factorial(5) << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cpp
./main 5: 0xbf9f6210
4: 0xbf9f61f0
3: 0xbf9f61d0
2: 0xbf9f61b0
1: 0xbf9f6190
5! = 120 grep 'model name' /proc/cpuinfo

model name : Pentium III (Katmai)

Evidently, my program stack grows "upward"
from the "bottom" of [virtual] memory (0xffffffff)
toward the "top" of [virtual] memory (0x00000000).
Oct 6 '05 #15
puzzlecracker wrote:
Mirek Fidler wrote:
EventHelix.com wrote:
This is dependent on the processor architecture. The code generator for
your C++ compiler would be using the stack organization that is
supported by the compiler.

For example, on the 68000 processor a push is a predecrement on the
stack pointer while the op is a post increment. Thus the stack grows by
decreasing the address. The following article describes this:


Actually, I think that all architectures have stack growing down today.

Mirek

MYy question is how to determine it programatically?


No portable way. You know, machine does not have stack at all and still
have C++ implementation. Or it can have more stacks. Or whatever.

Mirek
Oct 6 '05 #16
Mirek Fidler wrote:


No portable way. You know, machine does not have stack at all and still
have C++ implementation. Or it can have more stacks. Or whatever.

When I worked on the HEP, our "stack" was indeed a linked list.
Oct 6 '05 #17

Ron Natalie wrote:
Mirek Fidler wrote:


No portable way. You know, machine does not have stack at all and still
have C++ implementation. Or it can have more stacks. Or whatever.

When I worked on the HEP, our "stack" was indeed a linked list.


I don't see why there is a such controversy about machine/compiler
dependency in respect to system stack. Yet, a rather trivial matter is
overlooked. Stack - by definition- can either grow upwards or
downwards(pushing from the top or bottom). Correct?

In C++/C function return address is push onto to the stack (who cares
how stack is implemented) - making it most top element of the stack
(abstractly speaking). Calling the next function from within called
function would add called function address to the top of the stack, and
so on. That address can either increase or decrease - it will not be
the same (if it is the same: rm -rf compiler folder and guillotine
everyone in the group who developed it).
Again, where is the problem?

Oct 7 '05 #18
puzzlecracker wrote:
I don't see why there is a such controversy about machine/compiler
dependency in respect to system stack. Yet, a rather trivial matter is
overlooked. Stack - by definition- can either grow upwards or
downwards(pushing from the top or bottom). Correct?
I think there are some crossed wires here. You are talking about the way a
typical compiler generates code on a given machine so that a C++ program
will behave as the standard specifes. As others have pointed out, the
standard makes no mention of stacks. It just talks about function calls,
variables defined in various scopes etc. It's up to the compiler to make the
program behave correctly, in whatever way it chooses.
In C++/C function return address is push onto to the stack (who cares
how stack is implemented)
That's how a compiler usually chooses to do it, anyway.
- making it most top element of the stack
(abstractly speaking). Calling the next function from within called
function would add called function address to the top of the stack,
and so on. That address can either increase or decrease - it will not
be the same (if it is the same: rm -rf compiler folder and guillotine
everyone in the group who developed it).
Again, where is the problem?


No problem, but you are not talking about the C++ language. You are talking
about specific implementations. Strictly speaking, this whole thread is
off-topic.

DW
Oct 7 '05 #19
GB
E. Robert Tisdale wrote:
#include <iostream>

unsigned int factorial(unsigned int n) {
std::cerr << n << ": " << &n << std::endl;
return (1 < n)? n*factorial(n - 1): 1;
}

int main(int argc, char* argv[]) {
std::cout << "5! = " << factorial(5) << std::endl;
return 0;
}
> g++ -Wall -ansi -pedantic -o main main.cpp
> ./main 5: 0xbf9f6210
4: 0xbf9f61f0
3: 0xbf9f61d0
2: 0xbf9f61b0
1: 0xbf9f6190
5! = 120
> grep 'model name' /proc/cpuinfo

model name : Pentium III (Katmai)

Evidently, my program stack grows "upward"


According to your output, your stack grows downward (i.e., decreasing
addresses), not upward. The (virtual) addresses you printed are
decreasing with increased level of nesting (1 being the deepest nesting).
from the "bottom" of [virtual] memory (0xffffffff)
toward the "top" of [virtual] memory (0x00000000).


In what sense would 0xffffffff be the "bottom" of a presumably 32-bit
virtual memory space?

Gregg
Oct 7 '05 #20
GB wrote:
E. Robert Tisdale wrote:
Evidently, my program stack grows "upward"


According to your output, your stack grows downward (i.e., decreasing
addresses), not upward. The (virtual) addresses you printed are
decreasing with increased level of nesting (1 being the deepest
nesting).
> from the "bottom" of [virtual] memory (0xffffffff)
> toward the "top" of [virtual] memory (0x00000000).


In what sense would 0xffffffff be the "bottom" of a presumably 32-bit
virtual memory space?


Well, if you print out a block of memory from address zero it is likely that
the addresses will increase from the top of the page to the bottom, which
makes address zero the "top". However, I thought the convention was to call
zero the bottom and 0xffff.... the top.

DW
Oct 7 '05 #21
On Thu, 06 Oct 2005 21:17:57 +0200, Mirek Fidler <cx*@volny.cz> wrote
in comp.lang.c++:
EventHelix.com wrote:
This is dependent on the processor architecture. The code generator for
your C++ compiler would be using the stack organization that is
supported by the compiler.

For example, on the 68000 processor a push is a predecrement on the
stack pointer while the op is a post increment. Thus the stack grows by
decreasing the address. The following article describes this:


Actually, I think that all architectures have stack growing down today.


You're wrong to think that. There are some architectures (ARM, for
one), where you can choose the stack direction at compile time. Of
course, all source and libraries combined into a single executable
must be built with the same choice.

There are also some architectures (again including ARM), where you can
select big or little endian orientation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 7 '05 #22

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

Similar topics

26
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; }
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
9
by: shine | last post by:
what is the difference between a heap and a stack?
24
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...
20
by: deepak | last post by:
Hi All, In C I heard the stack grows from top to bottom and heap from bottom to top. Is it compiler depend?
7
by: puzzlecracker | last post by:
Dynamically allocated objects reside on a heap, local objects on a stack. What about static objects? Thanks
7
by: kr | last post by:
Hi All, Suppose I consider a sample program as given below:- #include<stdio.h> #include<stdlib.h> int i; int main() { char *test(int i); char *tmp = NULL;
43
by: Kislay | last post by:
Which of the following is correct regarding the storage of global variables : 1. Global variables exist in a memory area that exists from before the first reference in a program until after the...
25
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.