简体   繁体   English

需要说明此C程序中的内存地址如何工作

[英]need explanation of how memory address work in this C program

I have a very simple C program where I am (out of my own curiosity) investigating which memory addresses are used to allocate local variables. 我有一个非常简单的C程序,出于自身的好奇心,我正在调查哪个内存地址用于分配局部变量。 My program is: 我的程序是:

#include <stdio.h>

int main()
{
  char buffer_1[8], buffer_2[8], buffer_3[8];
  printf("address of buffer_1 %p\n", buffer_1);
  printf("address of buffer_2 %p\n", buffer_2);
  printf("address of buffer_3 %p\n", buffer_3);
  return 0;
}

output is as follows: 输出如下:

address of buffer_1 0x7fff5fbfec30
address of buffer_2 0x7fff5fbfec20
address of buffer_3 0x7fff5fbfec10

my question is: why do the address seem to be getting smaller? 我的问题是:为什么地址似乎越来越小? Is there some logic to this? 这有逻辑吗? thank you. 谢谢。

The compiler is allowed to do whatever it wants with your automatic variables. 允许编译器对您的自动变量执行任何所需的操作。 In this case it just looks like it's putting them consecutively on the stack. 在这种情况下,看起来就像是将它们连续放置在堆栈上。 On most popular systems in use today, stacks grow downwards. 在当今使用的大多数流行系统上,堆栈向下增长。

Most compilers allocate stack memory for local variables in one step, at the very beginning pf the function. 大多数编译器在函数开始的第一步就为本地变量分配了堆栈内存。 The memory is allocated as a single continuous block. 内存被分配为单个连续块。 Under these circumstances, the compiler, obviously, is free to use absolutely any memory layout for local variables inside that block. 在这种情况下,显然,编译器可以自由地使用任何内存布局作为该块内的局部变量。 If can put them there so that the addresses increase in the order of declaration. 如果可以将它们放在那里,以便地址按声明的顺序增加。 Or decrease. 或减少。 Or arranged randomly. 或随机安排。 It is an implementation detail. 这是一个实现细节。 And there's not much logic behind it. 而且背后没有太多逻辑。

It is quite possible that in your case the compiler tried to "pretend" that the memory for the arrays was allocated in the stack sequentially and independently (even though that was not the case). 在您的情况下,编译器很可能试图“假装”阵列的内存是顺序且独立地在堆栈中分配的(即使不是这种情况)。 If on your platform stack grows downwards (as it does on many platforms), then it is expected that object declared later will have smaller addresses. 如果在您的平台上堆栈向下增长(就像在许多平台上一样),则可以预期以后声明的对象将具有较小的地址。

But again, functions don't allocate local objects individually. 但是同样,函数不会单独分配本地对象。 And on top of that the language makes no guarantees about any relationships between local object addresses. 最重要的是,该语言无法保证本地对象地址之间的任何关系。 So, there's no real reason to prefer one ordering over the other. 因此,没有真正的理由更喜欢一种排序方式。

The output of your C program is platform-dependent, compiler-dependent. C程序的输出与平台有关,与编译器有关。 There cannot be just one perfect answer because the address arrangements vary based on: Whether the system is little or big endian. 不能仅有一个完美的答案,因为地址的安排根据以下因素而有所不同:系统是小字节序还是大字节序。 What kind of OS you are compiling on. 您正在编译哪种操作系统。 What kind of memory architecture you are compiling for. 您要编译哪种内存体系结构。 What kind of compiler you are using(and compilers might have bugs too) Whether you are on 64-bit or 32-bit platform. 您使用的是哪种类型的编译器(并且编译器也可能有错误),无论您使用的是64位还是32位平台。 And so much more. 还有更多。

But most important of all, is the type of processor architecture. 但最重要的是处理器架构的类型。 :) :)

Here is a list of stack growth strategies per processor: 以下是每个处理器的堆栈增长策略列表:

x86,PDP11    Downwards
System z     In a linked list fashion, downwards, mostly.
ARM          Select-able and can grow in either up or downward.
Mostek6502   Downwards (but only 256 bytes).
SPARC        In a circular fashion with a sliding window, a limited depth stack. 
RCA1802A     Subject to SCRT(Standard Call and Return Technique) implementation.

But, in general, your compiler, at compile-time should map those addresses into the binary file generated. 但是,通常,您的编译器在编译时应将这些地址映射到生成的二进制文件中。 Then at the run-time, the binary file may occupy(or may pretend to occupy) a sequential set of memory addresses. 然后,在运行时,二进制文件可能会占用(或伪装成会占用)一组连续的内存地址。 And in your case the addresses printed by your C source, show that the stack is growing downward. 在您的情况下,由C源打印的地址表明堆栈正在向下增长。

Basically compiler has responsibility to allocate memory to all the variables . 基本上,编译器负责为所有变量分配内存。 Array gets address on stack. 数组获取堆栈上的地址。 but it has nothing to do with the o/p you are getting. 但这与您获得的输出无关。 Basically The thing is compiler found the contiguous space(or chunk of memory) empty at that time and hence it allocated it to your program. 基本上,问题是编译器当时发现连续空间(或内存块)为空,因此将其分配给程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM