关于sizeof的疑问
我试图在 C++ 中使用 sizeof 运算符做一些事情。
请参考以下代码片段。
http://ideone.com//HgGYB
#include <iostream>
using namespace std;
int main()
{
int *pint = new int[5];
int temp = sizeof(*pint);
cout << "Size of the int array is " << temp << endl;
return 0;
}
我期望输出为 5*4 = 20。令人惊讶的是来到4.有什么想法吗?
I was trying to do something using sizeof operator in c++.
Please refer to the following code snippet.
http://ideone.com//HgGYB
#include <iostream>
using namespace std;
int main()
{
int *pint = new int[5];
int temp = sizeof(*pint);
cout << "Size of the int array is " << temp << endl;
return 0;
}
I was expecting the output as 5*4 = 20. Surprisingly it comes to 4. Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里的
pint
是一个int*
。因此,当编译器知道
sizeof(*pint)
时,它不知道new int[5]
(因为sizeof()
是一个编译时运算符)。[注意:尝试使用静态声明的数组
int pint[5];
进行相同的测试,将会看到预期的结果。此外,
sizeof()
返回size_t
(这是一个无符号值),因此它应该是:]
Here is
pint
is anint*
. So,Compiler doesn't know about
new int[5]
, when it doessizeof(*pint)
(becausesizeof()
is a compile time operator).[Note: Try the same test with statically declared array,
int pint[5];
and will see the expected result.Additionally,
sizeof()
returnssize_t
(which is an unsigned value), so it should be:]
动态大小的数组会丢失其大小信息 - 大小只是一个整数的大小,因为
pint
是指向 int 的指针,而*pint
是一个整数,而不是数组任何大小的类型。Dynamically sized arrays lose their size information- the size is only the size of one integer, as
pint
is a pointer to int, and*pint
is an integer, not an array type of any size.C++ 无法知道数组的大小。
在你的例子中,
返回一个 int,并且 sizeof(int) 在你的机器上是 4 。
There is no way for C++ to know the size of the array.
In your case,
returns an int, and sizeof(int) is 4 on your machine.
一切都很好。您要求
int
的大小为 4 个字节。All fine. You ask for the size of an
int
which will be 4 bytes.pint 是一个指向 int 的指针,恰好指向数组的开头。它不包含有关该数组的信息。
pint is a pointer to int that just happens to point to the beginning of an array. It contains no information about this array.
它为您提供指针指向的大小 - 数组中的第一个位置。
It is giving you the size of what the pointer points to - the first location in the array.
在当前情况下, sizeof(*pint) 给出的是 sizeof(int) ,因此它返回 4 。但是,即使您尝试 sizeof(pint),它也会返回指针的大小。如果您的计算机是 32 位计算机,则很可能是 4,否则如果是 64 位计算机,则可能是 8。
现在您已经问了,为什么它不返回 4*5 = 20。因为 pint 指向整数数组。是的,pint 指向一个整数数组,但它不是一个数组。区别在于:
由于 sizeof 运算符是在编译时评估的,因此编译器没有任何方法知道该指针指向的数组大小,因此无法告诉该对象的大小,因此它总是仅返回指针的大小。现在您可以理解,为什么在指针的情况下编译器给出指针的大小(即指针在内存中占用的空间),但在数组的情况下它给出完整的大小。
In the current case sizeof(*pint) is giving the sizeof(int), so its returning 4 . But, even if you will try sizeof(pint), it will return you the size of a pointer. Which will most probably be 4 if yours is a 32 bit machine else it will be 8, if 64 bit machine.
Now you have asked, why it is not returning 4*5 = 20. Since pint points to an integer array. Yes, pint points to an integer array, but its not an array. The difference is :
Since sizeof operator is evaluated at compile time, so compiler dont have any way to know at which size of array this pointer is pointing and so cant tell the size of that object and so it always return only the size of pointer. now you can understand, why in case of pointers compiler gives size of the pointer (ie space occupied by pointer in memory), but in case of array it gives full size.