2

I'm still learning C++. Currently I'm learning arrays and want to get length of an array. I know to get length of an array I have to type:

int length = (sizeof(array)/sizeof(*array));

It works very well. But, when I make a method to get length that I named as getLength() it doesn't work. Here my code:

#include <iostream>
using namespace std;

int array[5] = {1,2,3,4,5};

int getLength(int arg[]){
    return(sizeof(arg)/sizeof(*arg));
}

int main(){
    //array length
    cout << "Displaying array length" << endl;
    cout << getLength(array) << endl;
    system("pause");
}

That should returns 5, but it returned 1. If I use previous way, it returns 5, even they have same code.

Thanks for helps.

Sena
  • 178
  • 1
  • 13
  • Also: http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c "5.3 Pitfall" in particular. – eerorika Dec 31 '15 at 12:11
  • 1
    The answer to *all* your "how to use arrays in C++" is **don't**. Really. Use std::vector. – n. m. could be an AI Dec 31 '15 at 12:57
  • One way is to do it like [std::end does it](http://stackoverflow.com/a/33496357/1708801) using a template non-type parameter to deduce the array size. – Shafik Yaghmour Dec 31 '15 at 13:08
  • @ShafikYaghmour: That's not beginner-level C++, and besides a beginner should learn to use `std::end(arr) - std::begin(arr)`. See also http://stackoverflow.com/a/26947706/15416 – MSalters Dec 31 '15 at 14:12

3 Answers3

1

It's because array has decayed to a pointer type when it's passed to getLength.

So in that function, sizeof cannot be used to retrieve the total size of the array; it merely evaluates to the size of a pointer to the first element.

The normal thing to do in this instance is pass the size value in to getLength as a parameter.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Because when you pass the array to a function, the array decays to a pointer. So sizeof(arg) will return the size of the pointer on your architecture. If you need the size you will need to pass an extra parameter to the function. Check: What is array decaying?

Community
  • 1
  • 1
bashrc
  • 4,725
  • 1
  • 22
  • 49
  • so I can't use a function to get length. Thank you for your explanation :). I'll continue my study. – Sena Dec 31 '15 at 12:11
0

When you call a function with an array, the array decays to the pointer to the first element. The array syntax is quite misleading. The two functions below are equivalent:

void func1(int* p) { /* do something */ }
void func2(int p[]) { /* do something */ }

See this stack overflow answer for details.

What you can do is to delve into templates. Here's a function that does work:

template <typename T, size_t N>
size_t array_length(T (&arr)[N])
{
  return N;
}

The template captures an array (arr) by reference, and the size (N) is known by the template matching. Your main() example would work with it.

With that said, though, I think you'll be far far better off using std::vector<> or std::array<> instead. With those, the size is trivially accessible and you get other pleasant properties, not least that they do not decay to pointers.

Community
  • 1
  • 1
rollbear
  • 253
  • 2
  • 9