1

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.

Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29

3 Answers3

2

because customerListArray is a pointer

micheal.yxd
  • 118
  • 1
  • 7
2

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

fkl
  • 5,412
  • 4
  • 28
  • 68
1

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().

Kevin
  • 2,739
  • 33
  • 57
  • The `SizeOfArray` idiom will not help here. It is useful when the size of the array is static. And anyway, there is [`std::extent`](http://en.cppreference.com/w/cpp/types/extent), so why write it yourself? – BoBTFish Oct 10 '13 at 07:06
  • Sorry, but this is wrong. He/she's taking the size of `Customer**` which is a pointer type and in his particular platform pointers are 4 bytes long. `sizeof(Customer)` might not be 4. – Cassio Neri Oct 10 '13 at 07:07