[英]Why is sizeof(a) 16? (sizeof int is 4 )
#include <stdio.h>
int main() {
int *a[2]; // an array of 2 int pointers
int (*b)[2];
// pointer to an array of 2 int (invalid until assigned) //
int c[2] = {1, 2}; // like b, but statically allocated
printf("size of int %ld\n", sizeof(int));
printf("size of array of 2 (int *) a=%ld\n", sizeof(a));
printf("size of ptr to an array of 2 (int) b=%ld\n", sizeof(b));
printf("size of array of 2 (int) c=%ld\n", sizeof(c));
return 0;
}
a
is an array of 2 integer pointers, so shouldn't the size be 2 * 4 = 8
? a
是2个整数指针的数组,因此大小不应该为2 * 4 = 8
吗?
Tested on GCC. 在GCC上测试。
You're probably compiling on a 64-bit machine where pointers are 8 bytes. 您可能正在指针为8个字节的64位计算机上进行编译。
int *a[2]
is an array of 2 pointers. int *a[2]
是2个指针的数组。 Therefore sizeof(a)
is returning 16. 因此
sizeof(a)
返回16。
(So it has nothing to do with the size of an int
.) (因此,它与
int
的大小无关。)
If you compiled this for 32-bit, you'll mostly get sizeof(a) == 8
instead. 如果将其编译为32位,则大多数情况下将获得
sizeof(a) == 8
。
On 64-bit machines, pointers are usually 8 bytes. 在64位计算机上,指针通常为8个字节。 So the size of an array of two pointers is usually 16 bytes.
因此,两个指针组成的数组的大小通常为16个字节。
int *a[2]; // array of two pointers to int
int (*b)[2]; // pointer to an array of two int
sizeof a; // is 2 * 8 bytes on most 64-bit machines
sizeof b; // is 1 * 8 bytes on most 64-bit machines
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.