473,770 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stack increase direction and big-endian or little-endia

Hi,

1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS or compiler?

2. In GNU gcc,
{
int a[] = {0, 1, 2, 3, 4};
bool b;
float c;
for (int i = 0; i < 5; i++) {
cout << hex << a+i << "," ;
}
cout << endl;
cout << hex << &a << "," << &b << "," << &c << endl;
}
one print result is: 0x22ff40, 0x22ff44, 0x22ff48, 0x22ff4c, 0x22ff50
0x22ff40, 0x22ff3f, 0x22ff38
So considering question one, why the address of the array(a) increased to high address?
3. What decides the big-endian and little-endian, CPU architecture, OS or compiler?

4. {
int x = 0x12345678;
char *cp = (char*)&x;
for(int i = 0; i < 4; i++) {
cout << hex << (int)*(cp+i) << " ";
}
}
what is run result?
I think it depends on two aspects: stack increase direction and big-endian or little-endia.
Considering in the case of big-endian, suppose the address of x is 0x22ff44, namely &x=0x22ff44, and sizeof(x)=4.
If stack increases to low address, the variable x occupied 0x22ff44, 0x22ff43, 0x22ff42 and 0x22ff41, so the result is "78 56 34 12".
If stack increases to high address, the variable x occupied 0x22ff44, 0x22ff45, 0x22ff46 and 0x22ff47, so the result is "12 34 56 78".

Please give me some advice, thank you.
Oct 23 '05 #1
3 5192

gary wrote:
Hi,

1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS or compiler?

2. In GNU gcc,
{
int a[] = {0, 1, 2, 3, 4};
bool b;
float c;
for (int i = 0; i < 5; i++) {
cout << hex << a+i << "," ;
}
cout << endl;
cout << hex << &a << "," << &b << "," << &c << endl;
}
one print result is: 0x22ff40, 0x22ff44, 0x22ff48, 0x22ff4c, 0x22ff50
0x22ff40, 0x22ff3f, 0x22ff38
So considering question one, why the address of the array(a) increased to high address?
3. What decides the big-endian and little-endian, CPU architecture, OS or compiler?

4. {
int x = 0x12345678;
char *cp = (char*)&x;
for(int i = 0; i < 4; i++) {
cout << hex << (int)*(cp+i) << " ";
}
}
what is run result?
I think it depends on two aspects: stack increase direction and big-endian or little-endia.
Considering in the case of big-endian, suppose the address of x is 0x22ff44, namely &x=0x22ff44, and sizeof(x)=4.
If stack increases to low address, the variable x occupied 0x22ff44, 0x22ff43, 0x22ff42 and 0x22ff41, so the result is "78 56 34 12".
If stack increases to high address, the variable x occupied 0x22ff44, 0x22ff45, 0x22ff46 and 0x22ff47, so the result is "12 34 56 78".

Please give me some advice, thank you.


all three

Oct 23 '05 #2
gary wrote:
Hi,

1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS or compiler?
The direction that the stack and heap grows is OS-dependent and has
nothing to do with C or C++ compilers. Generally, one can expect that
the heap and the stack will grow in opposite directions and from
opposite ends of the logical address space (in order that they not
collide unnecessarily).

2. In GNU gcc,
{
int a[] = {0, 1, 2, 3, 4};
bool b;
float c;
for (int i = 0; i < 5; i++) {
cout << hex << a+i << "," ;
}
cout << endl;
cout << hex << &a << "," << &b << "," << &c << endl;
}
one print result is: 0x22ff40, 0x22ff44, 0x22ff48, 0x22ff4c, 0x22ff50
0x22ff40, 0x22ff3f, 0x22ff38
So considering question one, why the address of the array(a) increased to high address?
No matter which direction the heap or stack may grow, objects in
memory, including arrays will have the same layout. Each element in an
array will stored at a higher memory location than the element before
it.

3. What decides the big-endian and little-endian, CPU architecture, OS or compiler?
The CPU may only support one kind of endianness, or the OS may be
written for a particular endian-ness. Of course endianness is just a
convention for storing multibyte data. As such, a program is free to
use either convention (or make up its own) with its own data. Generally
in the interests of inter-operabillity and consistency, it is best to
use the same endianess that the OS prefers.
4. {
int x = 0x12345678;
char *cp = (char*)&x;
for(int i = 0; i < 4; i++) {
cout << hex << (int)*(cp+i) << " ";
}
}
what is run result?
On a big endian machine: 12 34 56 78
little-endian: 78 56 34 21
I think it depends on two aspects: stack increase direction and big-endian or little-endia.
The direction the stack increases has no effect on how x would be
stored in memory.
Considering in the case of big-endian, suppose the address of x is 0x22ff44, namely &x=0x22ff44, and sizeof(x)=4.
If stack increases to low address, the variable x occupied 0x22ff44, 0x22ff43, 0x22ff42 and 0x22ff41, so the result is "78 56 34 12".
If stack increases to high address, the variable x occupied 0x22ff44, 0x22ff45, 0x22ff46 and 0x22ff47, so the result is "12 34 56 78".


No, only the endianness used to store x would affect the order in which
its bytes are arranged in memory.

Greg

Oct 23 '05 #3
>1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth >decides their increase direction, CPU architecture, OS or compiler?

In most cases, it is the CPU architecture. In some cases where the
processor allows the stack to grow in either direction, the compiler
might provide a switch to choose between the two.
2. In GNU gcc,
{
int a[] = {0, 1, 2, 3, 4};
...
So considering question one, why the address of the array(a) increased to high address?
The space for the entire array is allocated in one stroke. Individual
array elements are not pushed on the stack. Note that C arrays operate
the same way regardless of the variables location on stack, heap or
global memory.

3. What decides the big-endian and little-endian, CPU architecture, OS or compiler?


In most cases, it is the CPU architecture. In some cases where the
processor supports little as well as big endian operation, the compiler
might provide a switch to choose between the two.

The following article should help:

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

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

Oct 23 '05 #4

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

Similar topics

7
7075
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so if that's your only reason not to help, please do. Otherwise, I guess it's OK. But, just remember, I'm not asking you to do my work for me, just to point out my error. My problem is not with the algorithm itself (standard divide and conquer on...
41
6093
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) {
2
5884
by: Scott Dabot | last post by:
Im trying to print out the order in which the vertexes of a graph are pushed on to the stack then print out the order in which they are popped off the stack. My current approach is void dfs(struct ADJACENCY *G_adj,int v) { int i,u; G_adj->mark=1; visit(v,G_adj); while ((u=next_adj_vertex(v,G_adj))>=0) {
2
3737
by: Ali | last post by:
Hi, I got stack overflow errors while using an unmanaged dll from managed c# application. When i use editbin.exe to increase stack size of app , it works. Is there a way to increase stack size of app (or most probably Clr) so that app starts with a larger stack, like using the old /F switch in Visual Studio C++ 6.0.
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.
11
2598
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating temperatures. T = 20 degrees at all times.... The 2D T-array looks like this:
9
7330
by: Ajay | last post by:
Hi all, Can I know what is the stack space and heap space allocated by the compiler.Can i increase it or decrease it.if yes,pleae tell me theway to do it.Thanks in advance. Cheers, Ajay
20
9210
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
22049
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char a; (this type of code and other complex mallc/free etc is used frequently and total approx 450 files, each file is of approx/avg 1000 line,
2
3050
by: PJ6 | last post by:
I have an algorithm that processes data recursively. When I'm testing it works fine, but when I feed it data from the actual application, I get "stack overflow". What bothers me is that the data my recursive processor is seeing in production is identical to the test data; the only difference is that now the processor must go deeper into an object model to get the values with which it is working. I trace things through, there is no out of...
0
9617
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
10257
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
10099
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
9904
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
7456
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.