sizeof 如何知道数组的大小? [英] How does sizeof know the size of array?

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

问题描述

我有以下代码:

main() {
    int array[5] = {3,6,9,-8,1};
    printf("the size of the array is %d\n", sizeof(array));
    printf("the address of array is %p\n", array);
    printf("the address of array is %p\n", &array);
    int * x = array;
    printf("the address of x is %p\n", x);
    printf("the size of x is %d\n", sizeof(x));
}

输出为

the size of the array is 20
the address of array is 0x7fff02309560
the address of array is 0x7fff02309560
the address of x is 0x7fff02309560
the size of x is 8

我知道变量 array 会被看作是指向数组第一个元素的指针,所以我理解 x 的大小是 8.但我不知道为什么数组是 20.它不应该是 8(在 64 位机器中)吗?

I know the variable array will be seen as a pointer to the first element of the array, so I understand the the size of x is 8. But I don't know why the size of the array is 20. Isn't it should be 8 (in a 64-bits machine)?

另外程序怎么知道它是20?据我所知,在 C 中它不存储元素的数量.为什么 sizeof(array)sizeof(x) 不同?我跟踪了几篇关于数组衰减的帖子,但不知道这个问题.

Besides how does the program know that it is 20? As far as I know in C it doesn't store the number of elements. How come the sizeof(array) and sizeof(x) is different? I tracked several posts pertaining to array decaying but no idea on this problem.

推荐答案

大多数情况下,数组的名称会衰减为指向数组第一个元素的指针.不过,该规则有几个例外.两个最重要的是当数组名称用作 sizeof 运算符或 address-of 运算符 (&) 的操作数时.在这些情况下,数组的名称仍然是整个数组的标识符.

The name of an array decays to a pointer to the first element of the array in most situations. There are a couple of exceptions to that rule though. The two most important are when the array name is used as the operand of either the sizeof operator or the address-of operator (&). In these cases, the name of the array remains an identifier for the array as a whole.

对于非 VLA 数组,这意味着可以静态确定数组的大小(在编译时)并且表达式的结果将是数组的大小(以字节为单位),而不是数组的大小指针.

For a non-VLA array, this means that the size of the array can be determined statically (at compile time) and the result of the expression will be the size of the array (in bytes), not the size of a pointer.

当您获取数组的地址时,您会得到相同的值(即相同的地址),就好像您只使用了数组的名称而不获取地址一样.但是类型是不同的——当你明确地获取地址时,你得到的是一个指向 T 类型的 N 个项目的数组的指针"类型的指针.这意味着(例如)array+1 指向数组的第二个元素,&array+1 指向另一个数组整个数组.

When you take the address of the array, you get the same value (i.e., the same address) as if you'd just used the name of the array without taking the address. The type is different though--when you explicitly take the address, what you get is a pointer of type "pointer to array of N items of type T". That means (for one example) that while array+1 points to the second element of the array, &array+1 points to another array just past the end of the entire array.

假设一个数组至少有两个元素,*(array+1) 将引用数组的第二个元素.无论数组大小如何,&array+1 都会产生超出数组末尾的地址,因此尝试取消引用该地址会产生未定义的行为.

Assuming an array of at least two items, *(array+1) will refer to the second element of the array. Regardless of the array size, &array+1 will yield an address past the end of the array, so attempting to dereference that address gives undefined behavior.

在你的例子中,假设数组的大小是 20,数组的一个元素的大小是 4,如果 array 是,比如说,0x1000,那么 array+1 将是 0x1004&array+1 将是 0x1014 (0x14 = 20).

In your case, given that the size of the array is 20, and the size of one element of the array is 4, if array was, say, 0x1000, then array+1 would be 0x1004 and &array+1 would be 0x1014 (0x14 = 20).

这篇关于sizeof 如何知道数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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