数组本身而言,它存储位置的名称 [英] The name of an array itself, where is it stored
问题描述
是如何存储在内存阵列的名字?
How are the names of arrays stored in memory?
例如,如果我写的:
char arr[10];
数组项存储在内存中的虚拟地址开始&放大器;常用3 [0]
这实际上是改编的值,但如果是改编
本身存储在哪里?
the array items are stored in memory starting from the virtual address &arr[0]
which is actually the value of arr, but where is arr
itself stored?
同样也适用于静态多维数组的行:
the same goes for the rows of static multidimensional arrays:
char arr[10][20];
改编本身也常用3 [0],ARR [1] ...改编[9]都是无地址本身
arr itself but also arr[0], arr[1]....arr[9] are all without addresses themselves.
那么,是名保存?
推荐答案
如果你的意思是在文字名称您使用宣布它,那一般是编出来,除非你带着调试符号编译 -g
。
If you mean the literal name that you declared it with, that is generally compiled out unless you compiled with debugging symbols -g
.
如果你声明在堆栈上的东西,机器code一般是指在数组元素作为帧指针偏移 $ EBP
。
If you declared something on the stack, the machine code generally refers to the array elements as an offset from the frame pointer $ebp
.
要回答你的问题暗示,对于在栈上的数组,不存在。该机code不知道是什么改编
是。它只知道的是,对所使用的偏移量(指数在code)解决存储区。
To answer your implied question, for an array on the stack, it does not exist. The machine code does not know what arr
is. It only knows that there is a region of memory that you are addressing using offsets (indices in your code).
的问题是为在堆上的阵列更有意义的,因为现在有一个指针(它有自己的地址),并保持该实际阵列存储器(这是在堆上,并存储在指针里面)
The question is more meaningful for an array on the heap, because now you have a pointer (which has its own address), and the memory which holds the actual array (which is on the heap, and stored inside the pointer).
考虑以下code。
char* arr = malloc(5);
假设你使用调试符号编译,如果你看看&放大器;改编
在 GDB
,你会看到地址存储在指针改编
在哪里。
Assuming you compiled with debug symbols, if you look at &arr
inside gdb
, you will see the address where the pointer arr
is stored.
如果你创建一个单独的指针在栈上的数组,可以表现出同样的事情。
You can demonstrate the same thing if you create a separate pointer to an array on the stack.
char arr[10];
char* ptr = arr;
在这里,你将看到 PTR
具有独立的存储( P&放大器; PTR
),并持有地址改编
作为它的价值,但&放大器; ARR
本身就是等于它的第一个元素的地址
Here, you will see that ptr
has separate storage (p &ptr
), and holds the address of arr
as its value, but &arr
itself is equal to the address of its first element.
这篇关于数组本身而言,它存储位置的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!