为什么返回的指针地址的十六进制值总是按递减顺序排列? 例如这里
int a
是在int d
之前声明的,所以它的地址总是大于d
,对于&b
,&e
和&c
,&f
也是如此,我想知道这是固定行为还是依赖于编译器?
我正在使用gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-1
)
#include<stdio.h>
int main(void){
int a=1;
int d=1;
char b='a' ;
char e='a';
float c=1.0;
float f=1.0;
printf("a=%p\nd=%p\nb=%p\ne=%p\nc=%p\nf=%p\n",&a,&d,&b,&e,&c,&f);
if (&a>&d)
printf("&a>&d\n");
else
{printf("&a<&d");
}
if (&a>&d && &b>&e && &c>&f)
printf("addresses are in descending order");
else{
printf("false");
}
return 0;
}
输出:
a=0xbfc6bd98 //a>d
d=0xbfc6bd94
b=0xbfc6bd9f //b>e
e=0xbfc6bd9e
c=0xbfc6bd90 //c>f
f=0xbfc6bd8c
&a>&d
addresses are in descending order
PS:我是c的新手
在 Aleph One 的 Smashing The Stack For Fun And Profit 中找到了很好的解释。 提取了最相关的部分。
/------------------\ lower
| | memory
| Text | addresses
| |
|------------------|
| (Initialized) |
| Data |
| (Uninitialized) |
|------------------|
| |
| Stack | higher
| | memory
\------------------/ addresses
Fig. 1 Process Memory Regions
[...]
The stack consists of logical stack frames that are pushed when calling a
function and popped when returning. A stack frame contains the parameters to
a function, its local variables, and the data necessary to recover the
previous stack frame, including the value of the instruction pointer at the
time of the function call.
Depending on the implementation the stack will either grow down (towards
lower memory addresses), or up. In our examples we'll use a stack that grows
down. This is the way the stack grows on many computers including the Intel,
Motorola, SPARC and MIPS processors.
[...]
Let us see what the stack looks like in a simple example:
example1.c:
------------------------------------------------------------------------------
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
}
void main() {
function(1,2,3);
}
------------------------------------------------------------------------------
[...]
With that in mind our stack looks like this when function() is called (each
space represents a byte):
bottom of top of
memory memory
buffer2 buffer1 sfp ret a b c
<------ [ ][ ][ ][ ][ ][ ][ ]
top of bottom of
stack stack
从 C 语言规范的角度来看,后续分配的变量的内存位置的顺序是未指定的。因此,这取决于...
some 编译器、some 架构和 some 编译器开关,您可能会看到这种行为,即局部变量被分配在连续较低的堆栈地址上,但优化和其他因素可以改变这种行为。
您所比较的地址的变量都是分配在堆栈上的局部变量。现在堆栈增长的方式(向上或向下)取决于架构。看起来在您的情况下,堆栈正在向下增长,因此您看到的地址正在减少。一个粗略且半准确的解释(忽略有关堆栈和堆差异的细节)是:您希望最大限度地减少内存中静态分配的内容与您需要动态分配的内容的冲突。当程序运行时,动态内容的大小通常会增加。为了最大化分配动态内容的容量,静态内容和动态内容通常分配在内存空间的两端,动态内容向静态内容增长。在您的具体情况下,编译器似乎将静态内容加载到内存中较低的位置,并从内存的末尾向开头增长动态内容(朝着静态内容的方向)。