473,396 Members | 1,995 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.

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 5168

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
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...
41
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...
2
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...
2
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...
18
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...
11
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...
9
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
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: 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...
2
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...
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...
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
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,...
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
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...

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.