Be careful of this:
hi we can get the maximam size of an array as follows
u can get the total memory of an array using sizeof()
then dividing by ur datatype memory size u will get the maximam no of elements in an array
This only works if the array is a local stack variable:
-
int arr[10];
-
int numelements = arr / sizeof(arr[0]); //calculates numelements as 10
-
However, if the array is a heap array:
[code=cpp]
int* arr = new int[10];
cout << sizeof( arr) / sizeof(arr[0]) << endl; //displays 1
That's because arr is a pointer. sizeof the pointer is 4. sizeof( arr[0]0 is als0 4. This situation occurs when you pass an array to a function. All that's passed is the address of the array. So inside a function sizeof( arr) / sizeof(arr[0] will always result in 1.
-
void fx(int* arr)
-
{
-
cout << sizeof arr / sizeof(arr[0]) << endl; //always displays 1.
-
}
-
-
int main()
-
{
-
int* arr = new int[10];
-
cout << sizeof arr / sizeof(arr[0]) << endl;
-
fx(arr);
-
}
-