6

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

4

5 に答える 5

6

これは、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 言語仕様の観点から、後で割り当てられる変数のメモリ位置の順序は規定されていません。したがって、それは依存します...

于 2012-09-15T15:13:08.167 に答える
3

これについては、なんの推測もできません。一部のコンパイラ、一部のアーキテクチャ、および一部のコンパイラ スイッチでは、この動作が見られる場合があります。つまり、ローカル変数が連続して低いスタック アドレスに割り当てられますが、最適化やその他の要因によってこの動作が変わる可能性があります。

于 2012-09-15T14:34:31.123 に答える
2

アドレスを比較している変数は、すべてスタックに割り当てられたローカル変数です。現在、スタックが成長する方法 (上向きまたは下向き) は、アーキテクチャによって異なります。あなたの場合、スタックが下向きに成長しているように見えるので、アドレスが減少しています。

詳細はこちら

于 2012-09-15T14:36:03.007 に答える
0

多くの ABI は、下方向に成長するスタックを定義します。

于 2012-09-15T14:35:36.423 に答える
0

全体的かつ半正確な説明 (スタックとヒープとの違いに関する詳細は無視) は次のとおりです。動的なものは通常、プログラムの実行時にサイズが大きくなります。動的なものを割り当てる容量を最大化するために、静的なものと動的なものは通常、メモリ空間の両端に割り当てられ、動的なものは静的なものに向かって成長します。あなたの特定の状況では、コンパイラがメモリの低い静的なものをロードし、動的なものをメモリの最後から最初に向かって(静的なものの方向に)成長させているように見えます。

于 2012-09-15T14:40:33.953 に答える