如何为C数组重新presented在记忆? [英] How Are C Arrays Represented In Memory?
问题描述
我相信我的理解变量和指针正常如何重新在内存psented如果您正在使用C. $ P $
I believe I understand how normal variables and pointers are represented in memory if you are using C.
例如,很容易理解,一个指针PTR将有一个地址,其价值将是一个不同的地址,这是空间它所指向的内存。下面code:
For example, it's easy to understand that a pointer Ptr will have an address, and its value will be a different address, which is the space in memory it's pointing to. The following code:
int main(){
int x = 10;
int *Ptr;
Ptr = &x;
return 0;
}
将有以下再presentation存储:
Would have the following representation in memory:
+---------------------+-------------+---------+
| Variable Name | Address | Value |
+---------------------+-------------+---------+
| x | 3342 | 10 |
+---------------------+-------------+---------+
| Ptr | 5466 | 3342 |
+---------------------+-------------+---------+
不过,我觉得很难理解数组是如何重新在内存psented $ P $。例如,code:
However I find it difficult to understand how arrays are represented in memory. For example the code:
int main(){
int x[5];
x[0]=12;
x[1]=13;
x[2]=14;
printf("%p\n",(void*)x);
printf("%p\n",(void*)&x);
return 0;
}
输出相同的地址两次(为简单起见10568的缘故)。意思是x ==&放大器; X。然而* X(或x [0]在数组符号)等于12,*(X + 1)(或x [1]在数组符号)等于13等。这怎么能重新presented?一种方式是这样的:
outputs the same address twice (for the sake of simplicity 10568). Meaning that x==&x. Yet *x (or x[0] in array notation) is equal to 12, *(x+1) (or x[1] in array notation) is equal to 13 and so on. How can this be represented? One way could be this:
+---------------------+-------------+----------+----------------------+
| Variable Name | Address | Value | Value IF array |
+---------------------+-------------+----------+----------------------+
| x | 10568 | 10568 | 12 |
+---------------------+-------------+----------+----------------------+
| | 10572 | | 13 |
+---------------------+-------------+----------+----------------------+
| | 10576 | | 14 |
+---------------------+-------------+----------+----------------------+
| | 10580 | | trash |
+---------------------+-------------+----------+----------------------+
| | 10584 | | trash |
+---------------------+-------------+----------+----------------------+
这是接近发生什么事情,或完全关闭?
Is this close to what happens, or completely off?
推荐答案
数组是与之间没有空格连续对象块。这意味着 X
在你的第二个例子是重新在内存psented为$ P $:
An array is a block of contiguous objects with no spaces in between. This means that x
in your second example is represented in memory as:
+---------------------+-------------+---------+
| Variable Name | Address | Value |
+---------------------+-------------+---------+
| x | 10568 | 12 |
| | +---------+
| | | 13 |
| | +---------+
| | | 14 |
| | +---------+
| | | ?? |
| | +---------+
| | | ?? |
+---------------------+-------------+---------+
这就是 X
为五 INT
的大,并且有一个地址。
That is, x
is five int
s big, and has a single address.
有关数组的怪异的一部分是不是在他们是如何存储 - 这是他们如何在EX pressions评估。如果使用数组名的地方,它不是一元&放大器的主题;
或的sizeof
运营商来说,计算为它的第一个成员的地址。
The weird part about arrays isn't in how they're stored - it's how they're evaluated in expressions. If you use an array name somewhere that it isn't the subject of the unary &
or sizeof
operators, it evaluates to the address of its first member.
也就是说,如果你只写 X
,你会得到一个价值10568型为int *
。
That is, if you just write x
, you will get a value 10568 with type int *
.
如果,在你写的另一方面&放大器; X
,则该特殊规则不适用 - 这样的&安培;
运营商如同它一般不会,这意味着它获取数组的地址。在这个例子中,这将是一个价值10568型 INT(*)[5]
。
If, on the other hand you write &x
, then the special rule doesn't apply - so the &
operator works like it normally does, which means that it fetches the address of the array. In the example, this will be a value 10568 with type int (*)[5]
.
这是 X ==&放的原因; X
是一个数组的第一个成员的地址是必然等于阵列本身的地址,因为数组开始与它的第一个成员。
The reason that x == &x
is that the address of the first member of an array is necessarily equal to the address of the array itself, since an array starts with its first member.
这篇关于如何为C数组重新presented在记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!