我正在定义我的结构,如:
struct Shape {
unsigned int l; // value for length
unsigned int h; // value or height
unsigned int b; // value for breadth
};
在我的程序中,我正在动态分配Shape的实例:
Shape *image = new Shape[i];
我正在尝试打印它的大小:
cout<< "The size of image is " << sizeof(image)<< ends;
不管i的值是多少,我都会得到相同的输出:(例如:0,1,2,10)
The size of the image is 8
为什么是这样?
注意:但是,当我将sizeof运算符应用于Shape结构时,就会得到正确的答案:
sizeof(Shape) -> returns 12
The size of the image is 8
为什么是这样?
输出正确,sizeof(image)
它为您提供了指针变量Shape*
本身的大小,而不是分配的数组的大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句