Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we specify arrays size as a parameter when passing to function in C++?

I searched this question, most of them says the same thing. Since we only pass the arrays address in a function, compiler can not know the arrays size by looking at the address, they say. I tried to test this by using this code, and both functions gave the same results. So, how does specifying the arrays size as a function parameter help me in a practical way?. In which conditions does specifying the size help us?.

class ArrayTest
{
    public:
     void say(int ar[])
     {
         cout<<ar[1]<<endl;
         cout<<ar[7]<<endl;
     }

      void say(int ar[],int sizeAn)
     {
         cout<<ar[1]<<endl;
         cout<<ar[7]<<endl;
     }

};

int main()
{
    ArrayTest test;
   int anAr[5] = {1,2,3,4,5};
   test.say(anAr);
   test.say(anAr,5);
    return 0;

}
like image 226
Crazy_Boy53 Avatar asked Oct 30 '18 09:10

Crazy_Boy53


People also ask

Why do we pass the size of the array to a function?

Passing the array size tells the function where the bounds are so you can choose not to go beyond them.

Do you have to specify the size of an array in C?

You can declare an array without a size specifier for the leftmost dimension in multiples cases: as a global variable with extern class storage (the array is defined elsewhere), as a function parameter: int main(int argc, char *argv[]) .

Do you need to specify size of array when you declare it?

Yes, it is necessary.

What happens when an array with a specified size is assigned in C?

If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer.


4 Answers

This is about you as a programmer having the chance to boundary check, not whether the compiler can do it.

Just try to print out all the elements in the array, with the size:

 void say(int ar[],int sizeAn)
 {
     for(int i=0; i< sizeAn; ++i)
         cout<<ar[i]<<endl;
 }

now without the size:

 void say(int ar[])
 {
     for(int i=0; i< /*HOW DO I KNOW NOW?*/; ++i)
         cout<<ar[i]<<endl;
 }
like image 95
PeterT Avatar answered Nov 10 '22 13:11

PeterT


Passing array size as a function parameter is a bad idea, because if you need an array as an array in function passing its size won't have any effect. The array you passed will be decayed to a pointer. So you need to maintain array as is.

Templates provide a simple and effective way to prevent array decay while passing them as function arguments.

template<std::size_t N>
void foo(int (&your_array)[N])
{
   for(int i = 0; i < N; i++)
      //process array, N will be your array size.
}
//simply pass array when calling the function. N be taken automatically.

//somewhere else
int main()
{
  int arr[10];
  foo(arr);
}

hope this helps.

like image 26
dev Avatar answered Nov 10 '22 12:11

dev


Note that your code is invoking undefined behavior because you're accessing element 7 of an array that is only 5 elements big. Using the size parameter, you could for instance check if the index is past its size and not do that call instead.

In your example, you get the same results becaue you aren't actually using the parameter:

 void say(int ar[],int sizeAn)
 {
     cout<<ar[1]<<endl;
     cout<<ar[7]<<endl;
 }

sizeAn is unused, so it's not making any difference. But consider for instance the following code:

void say(int ar[],int sizeAn)
     {
         for (int i = 0; i < sizeAn; i++){
             cout<<ar[i]<<endl;
         }
     }

Here, it's printing all the items in the array, so it needs to know how big the array is. If you used an std::vector, for instance, you wouldn't need to pass the size as you can just call the size function, but you can't do that with C style arrays, so you need to pass that size as a parameter if you want to write a function that behaves differently depending on the size).

Or here's a more practical example of your code where the size parameter is used to avoid the undefined behavior:

void say(int ar[],int sizeAn)
 {
     cout<<ar[1]<<endl;
     if (sizeAn >= 8){
         cout<<ar[7]<<endl;
     }
 }

Now it's the same as your code with the change that it's only printing the element 7 if it actually exists.

like image 20
Blaze Avatar answered Nov 10 '22 13:11

Blaze


As you say, compilers can't tell how big an array is if passed to a function. Your first say function tries to reference past the end of the array (ar[7] is beyond the size of 5). Your second say function means you can length check to make sure you don't make this error.

  void say(int ar[], int sizeAn) 
  {
     if(sizeAn>1)
        cout<<ar[1];endl;
     if(sizeAn>7)
        cout<<ar[7];endl;
  }

This way, YOU know the length and the function can check it before accessing invalid memory locations.

like image 37
Neil Avatar answered Nov 10 '22 12:11

Neil