How to compute size of an array parameter in a function?
Consider below C++ program:
// A C++ program to show that it is wrong to // compute size of an array parameter in a function #include <iostream> using namespace std; void findSize(int arr[]) { cout << sizeof(arr) << endl; } int main() { int a[10]; cout << sizeof(a) << " "; findSize(a); return 0; } |
Output:
40 8
The above output is for a machine where size of integer is 4 bytes and size of a pointer is 8 bytes.
The cout statement inside main prints 40, and cout in findSize prints 8. The reason is, arrays are always passed pointers in functions, i.e., findSize(int arr[]) and findSize(int *arr) mean exactly same thing. Therefore the cout statement inside findSize() prints size of a pointer. See this and this for details.
How to find size of array in function?
We can pass a ‘reference to the array’.
// A C++ program to show that we can use reference to // find size of array #include <iostream> using namespace std; void findSize(int (&arr;)[10]) { cout << sizeof(arr) << endl; } int main() { int a[10]; cout << sizeof(a) << " "; findSize(a); return 0; } |
Output:
40 40
The above program doesn’t look good as we have hardcoded size of array parameter. We can do it better using templates in C++.
// A C++ program to show that we use template and // reference to find size of integer array parameter #include <iostream> using namespace std; template <size_t n> void findSize(int (&arr;)[n]) { cout << sizeof(int) * n << endl; } int main() { int a[10]; cout << sizeof(a) << " "; findSize(a); return 0; } |
Output:
40 40
We can make a generic function as well:
// A C++ program to show that we use template and // reference to find size of any type array parameter #include <iostream> using namespace std; template <typename T, size_t n> void findSize(T (&arr;)[n]) { cout << sizeof(T) * n << endl; } int main() { int a[10]; cout << sizeof(a) << " "; findSize(a); float f[20]; cout << sizeof(f) << " "; findSize(f); return 0; } |
Output:
40 40 80 80
Now the next step is to print the size of a dynamically allocated array. It’s your task man ! I’m giving you a hint.
#include <iostream> #include <cstdlib> using namespace std; int main() { int *arr = (int*)malloc(sizeof(float) * 20); return 0; } |
This article is contributed Swarupananda Dhua Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to pass a 2D array as a parameter in C?
- Parameter Passing Techniques in C/C++
- Difference between Argument and Parameter in C/C++ with Examples
- Get the stack size and set the stack size of thread attribute in C
- How to find size of array in C/C++ without using sizeof ?
- array::size() in C++ STL
- size of char datatype and char array in C
- Generate an array of given size with equal count and sum of odd and even numbers
- How does free() know the size of memory to be deallocated?
- Extended Integral Types (Choosing the correct integer size in C/C++)
- How to quickly swap two arrays of same size in C++?
- list::empty() and list::size() in C++ STL
- queue::empty() and queue::size() in C++ STL
- stack empty() and stack size() in C++ STL
- deque::empty() and deque::size() in C++ STL
- priority_queue::empty() and priority_queue::size() in C++ STL
- map::size() in C++ STL
- set::size() in C++ STL
- C Program to find size of a File
- list size() function in C++ STL

