连续声明的变量的内存地址顺序是否总是降序? [英] Is the order of memory addresses of successively declared variables always descending?

查看:54
本文介绍了连续声明的变量的内存地址顺序是否总是降序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么返回的指针地址的十六进制值总是降序?例如这里 int a 是在 int d 之前声明的,所以它的地址总是大于 d,对于 也是一样&b,&e&c,&f,我想知道这是一个固定的行为还是这个编译器依赖?我正在使用 gcc 版本 4.4.5 (Ubuntu/Linaro 4.4.4-1)

#includeint main(void){int a=1;整数 d=1;字符 b='a' ;字符 e='a';浮动 c=1.0;浮动 f=1.0;printf("a=%p\nd=%p\nb=%p\ne=%p\nc=%p\nf=%p\n",&a,&d,&b,&e,&c,&f);如果(&a>&d)printf("&a>&d\n");别的{printf("&a<&d");}如果(&a>&d && &b>&e && &c>&f)printf("地址按降序排列");别的{printf("假");}返回0;}

输出:

a=0xbfc6bd98//a>dd=0xbfc6bd94b=0xbfc6bd9f//b>ee=0xbfc6bd9ec=0xbfc6bd90//c>ff=0xbfc6bd8c&a>&d地址按降序排列

PS:我是 c 的新手

解决方案

Smashing The Stack 中找到了很好的解释为了乐趣和利润,Aleph One.提取了最相关的部分.

<小时><块引用>

/-------------------\ 下||记忆|文字 |地址|||-------------------||(已初始化) ||数据 ||(未初始化) ||-------------------||||堆栈 |更高||记忆\-----------------/地址图 1 进程内存区域

[...]

<块引用>

 栈由逻辑栈帧组成,当调用一个函数并在返回时弹出.堆栈帧包含以下参数一个函数、它的局部变量和恢复数据所需的数据前一个堆栈帧,包括指令指针的值函数调用的时间.根据实现,堆栈将向下增长(朝向较低的内存地址),或向上.在我们的示例中,我们将使用一个不断增长的堆栈下.这是堆栈在包括英特尔在内的许多计算机上增长的方式,摩托罗拉、SPARC 和 MIPS 处理器.

[...]

<块引用>

 让我们在一个简单的例子中看看堆栈是什么样的:示例1.c:-------------------------------------------------------------------------------无效函数(int a,int b,int c){字符缓冲区1[5];字符缓冲区2[10];}无效主(){功能(1,2,3);}-------------------------------------------------------------------------------

[...]

<块引用>

考虑到这一点,当 function() 被调用时,我们的堆栈看起来像这样(每个空格代表一个字节):顶部底部记忆记忆缓冲区 2 缓冲区 1 sfp ret a b c<------ [ ][ ][ ][ ][ ][ ][ ]顶部底部栈栈

<小时>

如你所见,新的(局部)变量被压入栈顶.根据架构的设计,堆栈向更高的内存地址或向更低的内存地址增长,在您的情况下是后者.

从 C 语言规范的角度来看,后续分配的变量的内存位置顺序是未指定的.因此,这取决于...

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? I am using 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

解决方案

Found this nicely explained in Smashing The Stack For Fun And Profit, by Aleph One. 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   a     b     c
<------   [            ][        ][    ][    ][    ][    ][    ]

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. Therefore, it depends ...

这篇关于连续声明的变量的内存地址顺序是否总是降序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆