C ++数组大小始终为4 [英] C++ array size is always 4

查看:117
本文介绍了C ++数组大小始终为4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标头中定义了一个数组

Hi I have an array defined in my header filed

private:
    Customer** customerListArray;

在我的cpp文件中,将其设置如下,

In my cpp file I set it as following,

customerListArray = new Customer* [data.size()];
cout << "arr size " << data.size() << "\n";
cout << "arr size " << sizeof(customerListArray) << "\n";

但是data.size()是11900,但是sizeof(customerListArray)数组始终是4.我尝试用100替换data.size(),但仍然得到4.

However data.size() is 11900, but sizeof(customerListArray) array is always 4. I've tried replacing data.size() with 100 and still I get 4.

我在做什么错了?

谢谢.

推荐答案

指针始终具有固定大小,并且OP正在使用指针.为了使 sizeof()返回数组的实际长度,您必须声明一个数组并将其名称传递给sizeof().

Pointers are always of fixed size and the OP is using pointer. For sizeof() to return the actual length of an array, you have to declare an array and pass it's name to 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()返回的是字节数,而不是元素数

It is also important to note that sizeof() returns number of bytes and not number of elements

这篇关于C ++数组大小始终为4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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