This question already has answers here:
Closed 6 years ago.
Hi I have an array defined in my header filed
private:
Customer** customerListArray;
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";
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.
What am I doing wrong here?
Thank you.
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.
It is also important to note that sizeof() returns number of bytes and not number of elements
because customerListArray
is a pointer
sizeof() returns the size in bytes of an element, in this case your 'customer**' is 4 bytes in size.
See this page for reference on sizeof().