嗨,我在标头字段中定义了一个数组
private:
Customer** customerListArray;
在我的cpp文件中,将其设置如下:
customerListArray = new Customer* [data.size()];
cout << "arr size " << data.size() << "\n";
cout << "arr size " << sizeof(customerListArray) << "\n";
但是data.size()为11900,但是sizeof(customerListArray)数组始终为4。我尝试将data.size()替换为100,但仍然得到4。
我在这里做错了什么?
谢谢你。
指针的大小总是固定的,OP使用的是指针。为了使sizeof()返回数组的实际长度,您必须声明一个数组并将其名称传递给sizeof()。
int arr[100];
sizeof(arr); // This would be 400 (assuming int to be 4 and num elements is 100)
int *ptr = arr;
sizeof(ptr); // This would be 4 (assuming pointer to be 4 bytes on this platform.
同样重要的是要注意sizeof()返回的是字节数,而不是元素数
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句