C 数组如何在内存中表示? [英] How Are C Arrays Represented In Memory?

查看:25
本文介绍了C 数组如何在内存中表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用 C,我相信我理解普通变量和指针在内存中的表示方式.

I believe I understand how normal variables and pointers are represented in memory if you are using C.

例如,很容易理解一个指针Ptr会有一个地址,它的值会是一个不同的地址,也就是它指向的内存空间.代码如下:

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;
}

在内存中会有以下表示:

Would have the following representation in memory:

+---------------------+-------------+---------+
| Variable Name       | Address     | Value   | 
+---------------------+-------------+---------+
| x                   | 3342        | 10      |
+---------------------+-------------+---------+
| Ptr                 | 5466        | 3342    |
+---------------------+-------------+---------+

然而,我发现很难理解数组在内存中的表示方式.例如代码:

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,依此类推.这如何表示?一种方法可能是这样的:

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 在内存中表示为:

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 ints big, and has a single address.

关于数组的奇怪部分不在于它们的存储方式——而是它们在表达式中的计算方式.如果您在某处使用不是一元 &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,你会得到一个类型为int *的值10568.

That is, if you just write x, you will get a value 10568 with type int *.

另一方面,如果您编写 &x,则特殊规则不适用 - 因此 & 运算符的工作方式与通常一样,即意味着它获取数组的地址.在示例中,这将是一个类型为 int (*)[5] 的值 10568.

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 数组如何在内存中表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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