0

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]);

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.