SaschArt
SaschArt

Reputation: 11

Get size of array pointers c++

How to get the size of array of pointers made it with new double[...]

Using this is works

double arr[10];
sizeof(arr)/sizeof(arr[0]);

but this is not

double *arr;
arr=new double[10];
sizeof(arr)/sizeof(arr[0]);

Upvotes: 0

Views: 64

Answers (1)

user7015283
user7015283

Reputation:

Initially you declared a double array of size 10.

double arr[10];

And then this statement gives number of array elements in the array. Yes right.

sizeof(arr)/sizeof(arr[0]);

In other programme this statement ....

 double *arr;

creates a pointer to double.

And this...

arr=new double[10];

stores the starting address of array in arr. But arr is still a pointer to double.

This statement ...

sizeof(arr)/sizeof(arr[0]);

is the culprit. Here sizeof(arr) returns size of the pointer to double instead of size of array. And sizeof(arr[0]) returns the size of double. Depending on your machine your size of pointer and size of double may be different. But it will print the quotient of the two values.

Upvotes: 2

Related Questions