What is Array Decay in C++ & how to prevent it?

What is Array Decay in C++ & how to prevent it?
What is Array Decay in C++ & how to prevent it?

In this article, we will learn about the concept of array decay in C++ and the various methods by which it can be prevented.

The loss of size and dimensions of our array is called an array decay. It can lead to problems during programming as the size of the original array does not remain the same.

Occurrence of Array Decay in C++

The concept of array decay arises when we pass our array as a pointer to a function in the programme. The first address of an array is sent which is a pointer. Hence, the size of the array is the one occupied by the pointer in memory.

Example:

#include<iostream>

using namespace std;

// Driver function to show Array decay
// Passing array by value
void aDecay(int *p)
{
// Printing size of pointer
cout << “Modified size of array is by “
” passing by value: “;
cout << sizeof(p) << endl;
}

// Function to show that array decay happens
// even if we use pointer
void pDecay(int (*p)[7])
{
// Printing size of array
cout << “Modified size of array by “
“passing by pointer: “;
cout << sizeof(p) << endl;
}

int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7,};

// Printing original size of array 
cout << "Actual size of array is: "; 
cout << sizeof(a) <<endl; 

// Calling function by value 
aDecay(a); 

// Calling function by pointer 
pDecay(&a); 

return 0; 

}

Output:

Actual size of array is: 28
Modified size of array by passing by value: 8
Modified size of array by passing by pointer: 8

In the above code, the actual size of our array was 28 but when passed using pointer it decays down to 8 i.e, only the size of one pointer. This creates a problem to several programmers that why only one element of the array is accessed.

So let us now move ahead the prevention methods of this decay.

  • Pass the size of the array: One way is to pass the array size as a function parameter. If the size will be known then decay won’t happen.

Example:

#include<iostream>

using namespace std;
// n is the size of original array passed
void func( int arr[], int n)
{
// Prints the array
for(int i = 0; i < n; i++)
cout << arr[i] << “ “;
}

      int main()
 {
    int arr[5] = { 1, 2, 3, 4, 5};
    func(arr,5);
    return 0;
  }

Note
Size of the array can be calculated by the method
Size = sizeof(arr) / sizeof(arr[0]) Here,

sizeof(arr)- returns the number of bytes the array occupies
sizeof(arr[0]) – returns the size of one element

  • Pass by reference: Another way to prevent the array decay is to pass the array into function. It prevents the conversion of an array to a pointer.

Example:

#include<iostream>

using namespace std;

// A function that prevents Array decay
// by passing array by reference
void fun(int (&p)[7])
{
// Printing size of array
cout << “Modified size of array by “
“passing by reference: “;
cout << sizeof(p) << endl;
}

int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7,};

// Printing original size of array 
cout << "Actual size of array is: "; 
cout << sizeof(a) <<endl; 

// Calling function by reference 
fun(a); 

return 0; 

}

Conclusion:

  • An array decay is a loss of dimensions of an array.
  • It occurs due to a copy of the pointer into functions.
  • It can be prevented by two ways – pass the size of an array, pass the array as reference.

To read more about Arrays, click here.

By Mansi Agarwal