Home > database >  find size of parameterized array
find size of parameterized array

Time:11-30

I'm simply trying to get the size of an array passed as a parameter in a function, but I dont understand why the sizeof(k)/sizeof(k[0]) just returns one, when it works fine in the scope where it was declared, what am i missing here?

Heres the code:

#include <iostream>

using namespace std;

int fn(int k[]){
    
    cout << "size in function :" << sizeof(*k) / sizeof(k[0]) << endl; //returns 1 for some reason    
    //cout << "size in function :" << end(k)-begin(k) << endl; // can't find begin-end fitting function?


    int m = *max(&k[0], &k[sizeof(k)/sizeof(int)]);
    return m;
}

int main()
{
    int k[] = { 1,2,3,4,5,6,7 };
    int s = size(k);
    cout << "size :" << sizeof(k) / sizeof(k[0]) << endl;
    cout << "max: " << fn(k);

    return 0;
}

CodePudding user response:

what am i missing here?

The type of the parameter is int* (after having been adjusted from an array of unknown bound).

sizeof(k)/sizeof(int)

You're dividing the size of a pointer with the size of an integer. That division has nothing to do with the size of the array whose element is being pointed at.

when it works fine in the scope where it was declared

That's because the type of that k isn't int*. The type of the that k is int[7].

find size of parameterized array

The type of a parameter is never an array in C . If you declare a function parameter to be an array, then the type will be adjusted to be a pointer to element of such array.

If the type of the parameter is pointer, and if that pointer points to element of an array, then there is no general way to find out the size of the array in question.

Some arrays contain a sequence that is terminated by an element with a sentinel value. You can determine the length of such sequence by performing a linear search for the sentinel value. Null terminated strings are a common example of such sequences.

Old fashioned, C style is to pass the size of the array as separate parameter:

int fn(int k[], std::size_t size);
// usage
int k[] = { 1,2,3,4,5,6,7 };
fn(k, std::size(k));

A more modern approach is to combine the pointer and the size in a class. There is a class template for such purpose in the standard library:

 int fn(std::span<int> k);
 // usage
 int k[] = { 1,2,3,4,5,6,7 };
 fn(k);

CodePudding user response:

The size of an array only "exists" at compile-time. By the time the code is compiled, arrays are nothing more than pointers.

So if you want a function to accept a sized array, then the size needs to be passed at compile-time, which means it has to be a template parameter.

template<std::size_t N>
int fn(int (&k)[N]){
    cout << "size in function :" << N << endl;
    cout << "size in function :" << end(k)-begin(k) << endl;

    int m = *max(begin(k), end(k));
    return m;
}
  •  Tags:  
  • c
  • Related