简体   繁体   English

连续声明的变量的内存地址顺序是否总是在下降?

[英]Is the order of memory addresses of successively declared variables always descending?

why does the hexadecimal value of pointer address returned is always in decreasing order? 为什么返回的指针地址的十六进制值总是按降序排列? for example here int a was declared before int d , so it's address always comes out to be greater than d , and same for &b , &e and &c , &f , I want to know that is this a fixed behavior or is this compiler dependent? 例如, int aint d之前声明,所以它的地址总是大于d ,对于&b&e&c&f ,我想知道这是一个固定的行为还是这个编译器依赖? I am using gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-1 ) 我使用的是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;

}

output: 输出:

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: I am new to c PS:我是c的新手

Found this nicely explained in Smashing The Stack For Fun And Profit, by Aleph One . Aleph One的Smashing The Stack For Fun和Profit中找到了这个很好的解释。 Extracted the most relevant parts. 提取了最相关的部分。


  /------------------\\ 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 abc <------ [ ][ ][ ][ ][ ][ ][ ] top of bottom of stack stack 

As you can see, new (local) variables are pushed on top of the stack. 如您所见,新的(本地)变量被推到堆栈顶部。 Depending on the design of the architecture the stack grows towards higher memory addresses or towards lower memory addresses, the latter in your case. 根据体系结构的设计,堆栈会朝着更高的内存地址或更低的内存地址增长,后者就是你的情况。

From the viewpoint of the C language specification the order of memory locations of subsequently allocated variables is unspecified. 从C语言规范的角度来看,未指定随后分配的变量的存储器位置的顺序。 Therefore, it depends ... 因此,这取决于......

You can't make any assumptions about this. 你不能对此作出任何假设。 With some compilers, some architectures and some compiler switches you may see ths behaviour, ie local variables being allocated at successively lower stack addresses, but optimisation and other factors can change this behaviour. 对于一些编译器, 一些体系结构和一些编译器开关,您可能会看到行为,即本地变量在连续较低的堆栈地址处分配,但优化和其他因素可以改变此行为。

The variables whose address you are comparing are all local variables allocated on stack. 您要比较的地址变量是堆栈上分配的所有局部变量。 Now the way a stack grows (upwards or downwards) depends on the architecture. 现在堆栈增长(向上或向下)的方式取决于架构。 Looks like in your case the stack is growing downwards, so you are seeing decreasing addresses. 看起来在你的情况下堆栈正在向下增长,所以你看到地址递减。

More on this here 更多关于这里

很多ABI定义了一个向下增长的堆栈。

A gross and semi-accurate explanation (disregarding details about differences with stacks and heaps) is: You want to minimize the statically allocated stuff in memory colliding with the stuff you need to allocate dynamically. 一个粗略和半准确的解释(忽略有关堆栈和堆的差异的详细信息)是:您希望最小化内存中静态分配的内容与您需要动态分配的内容冲突。 The dynamic stuff typically grows in size when your program runs. 当程序运行时,动态内容通常会增大。 To maximize the capacity of allocating dynamic stuff, the static stuff and dynamic stuff are typically allocated at opposite ends of your memory space, with the dynamic stuff growing towards your static stuff. 为了最大化分配动态内容的能力,静态内容和动态内容通常分配在内存空间的两端,动态内容会逐渐增加到静态内容。 In your specific situation it looks like your compiler loads the static stuff low in memory and grows the dynamic stuff from the end towards the beginning of your memory (in the direction of your static stuff). 在您的特定情况下,您的编译器似乎会加载内存中较少的静态内容,并从内存到内存开头(在静态内容的方向上)增加动态内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么连续声明的指针变量以其地址的降序存储在内存中? - Why are successively declared pointer variables stored in memory in a decreasing order of their addresses? 为什么在不同范围内使用相同名称声明的变量会被分配相同的内存地址? - Why do variables declared with the same name in different scopes get assigned the same memory addresses? 为什么mmap()以降序返回地址,而malloc()以升序返回? - Why does mmap() return addresses in descending order, and malloc() in ascending order? 为什么内存地址在一个地方上升而在另一个地方下降? - Why are the memory addresses ascending in one place and descending in another? 变量的内存地址之间的差异是恒定的 - Difference between memory addresses of variables is constant 编译器如何为变量分配内存地址? - how do compilers assign memory addresses to variables? 检查并比较不同类型变量的内存地址 - Checking and comparing memory addresses for variables of different type 变量在 C 的哪个阶段用 memory 声明和分配? - variables are declared and allocated with memory in which stage in C? 分别声明和初始化的变量如何在内存中工作? - How separately declared and initialized variables work in memory? 声明变量的顺序有区别吗? - Does the order in which variables are declared makes a difference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM