2

Given the following function:

template<class T>
void foo(T* ar)
{
    std::cout << sizeof(ar) / sizeof(ar[0]) << std::endl;
    for (int i = 0; i < 6; i++)
        std::cout << ar[i] << " ";
}

And following array:

int ar[] = { 5, 1, 6, 8, 9, 3 };
foo(ar);

I expected the program to print '6' and then the contents of the array. But for some reason, sizeof(ar) / sizeof(ar[0]) evaluates to 1 (since both sizeof(ar) and sizeof(ar[0]) evaluate to '4'), but the print works fine (meaning passed array does contain all 6 elements).

This only happens to arrays passed to functions (I tried evaluating length in main(), where array was declared and it worked fine). How can I get the length of an array inside a function?

6
  • 3
    You cannot get the length through a pointer. That's why using std::vector is the preferred way of passing arrays Commented Apr 21, 2020 at 8:37
  • @UnholySheep, so if I have to work with arrays only way would be to pass the length to the function as well?
    – Ach113
    Commented Apr 21, 2020 at 8:38
  • 2
    The array ar decays to a pointer when passed to a function, so ar in foo is just a pointer Commented Apr 21, 2020 at 8:39
  • 2
    You should pass an array_view, or pass array by reference, or pass size explicitly. And never ever use something like sizeof(ar) / sizeof(ar[0]) to figure out size of the array, use ::std::size instead. Commented Apr 21, 2020 at 8:40

1 Answer 1

3

Just declare the parameter as having a referenced type.

For example

template<class T>
void foo( const T &ar )
{
    size_t n = sizeof(ar) / sizeof(ar[0]);
    std::cout << n << std::endl;
    for (int i = 0; i < n; i++)
        std::cout << ar[i] << " ";
}

Or you may use the following declaration

template<class T, size_t N>
void foo( const T ( &ar )[N] )
{
    std::cout << N << std::endl;
    for (int i = 0; i < N; i++)
        std::cout << ar[i] << " ";
}

Otherwise the function deals with a pointer. In this case you need specify a second parameter that denotes the length of the array like

template<class T>
void foo( const T* ar, size_t n )
{
    std::cout << n << std::endl;
    for (int i = 0; i < n; i++)
        std::cout << ar[i] << " ";
}

As the passed array is not being changed in the function then the corresponding parameter should have the qualifier const.

Pay attention to that in the C++ 17 Standard there is introduced standard function std::size declared in the header <iterator> that can be used instead of the expression sizeof(ar) / sizeof(ar[0])

So you could write for example in the first shown function

size_t n = std::size(ar)`;
6
  • I think I will go with the first one, void foo( const T &ar ), but in this case type T becomes array. Is there any way to get the type of the element of that array?
    – Ach113
    Commented Apr 21, 2020 at 8:58
  • @Ach113 You can include the header <type_traits> and use something like typename std::remove_extent_t<std::remove_reference_t<decltype( ar )>> x; Commented Apr 21, 2020 at 9:07
  • @Ach113 You can this long record make shorter by using an alias declaration. For example using Type = typename std::remove_extent_t<std::remove_reference_t<decltype( ar )>>; Type x = 10; Commented Apr 21, 2020 at 9:10
  • thing is, I want to create an object of that type SomeObject<T>. I need to pass a datatype like int to it. But if T becomes an array, what could I do in this case?
    – Ach113
    Commented Apr 21, 2020 at 9:12
  • 1
    @Ach113 You can declare the function like template<class T> void foo( const T &ar, typename std::remove_extent_t<T> x ); Commented Apr 21, 2020 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.