My function receives an array as a parameter but the array decays to a pointer so sizeof() doesn't work. I'm not allowed to import any extra libraries and the array was passed as int arr[]. when I searched it up, most people just say to define it outside of the function and pass it in as an argument but I can't do that. so is there a way for me to get the array size?
-
5short answer: no, it is not possible – OrenIshShalom Oct 24 '19 at 07:35
-
Simple answer: No, you can't without additional information. You have a pointer which points to a "random" address in memory, the pointer cannot know, how much following memory is "valid" – RoQuOTriX Oct 24 '19 at 07:35
-
1Long answer: see https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array – Tobias Brösamle Oct 24 '19 at 07:35
-
Possible duplicate of [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – Tobias Brösamle Oct 24 '19 at 07:36
-
Possible duplicate of [When a function has a specific-size array parameter, why is it replaced with a pointer?](https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p) – Sander De Dycker Oct 24 '19 at 07:40
-
Alternatively : [determine size of array if passed to function](https://stackoverflow.com/questions/968001/determine-size-of-array-if-passed-to-function) – Sander De Dycker Oct 24 '19 at 07:42
-
1Unless the array has some structure, such as a "magic" value to terminate it, you can't. I suspect that there is some vital information in your requirements that you have missed. – molbdnilo Oct 24 '19 at 07:43
-
The "magic" value is called a *"Sentinel"*. Pick a number outside the range of your normal data (like `INT_MIN`, etc..) and use that to mark the end of the `int` array the same way `'\0'` marks the end of a c_str(). – David C. Rankin Oct 24 '19 at 07:52
-
Related question: [Finding size of dynamically allocated array](https://stackoverflow.com/questions/12617244/finding-size-of-dynamically-allocated-array) – rustyx Oct 24 '19 at 08:09
1 Answers
There are three ways (well two really, as two of the ways are basically the same just with different classes):
- Use
std::vector
instead of array and use thesize
function to get the number of elements - Use
std::array
instead (which is really the same as the first) Use templates and pass the array by reference:
template<size_t N> void my_function(int (&my_array)[N]) { std::cout << "The number of elements in the array is " << N << '\n'; }
If you let the array decay to a pointer, all that you have is that pointer to a single element of your array. There's no information about the array itself, like its size. When using pointers you need to pass the size as a separate argument.
If you're not allowed to change the function signature, and the function only takes a pointer as an argument, there's really no way to get the size of the array. For an interview question this is probably the answer you should give.
If it's an assignment given at school or similar you have to talk to the teacher and tell them it's impossible, or come up with an algorithm where you don't need the size explicitly (like adding some kind of terminator to the array, similar to the way that C-style strings are terminated).

- 400,186
- 35
- 402
- 621
-
1I understood the OP's not allowed to change the parameters, so I don't think this is really a solution. – Tobias Brösamle Oct 24 '19 at 07:39