length of dynamic array in c++
24,726
Solution 1
Because arr
is not an array, but a pointer, and you are running on an architecture where size of pointer is equal to the size of int
.
Solution 2
Because sizeof
does not work for dynamic arrays. It gives you the size of pointer
, since int *arr
is a pointer
You should store the size of allocated array or better use std::vector
Comments
-
Jatin almost 4 years
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)I declared a dynamic array like this:
int *arr = new int[n]; //n is entered by user
Then used this to find length of array:
int len = sizeof(arr)/sizeof(int);
It gives
len
as1
instead ofn
. Why is it so?