- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Array Decay in C++? How can it be prevented?
Here we will see what is Array Decay. The loss of type and dimensions of an array is called array decay. It occurs when we pass the array into a function by pointer or value. The first address is sent to the array which is a pointer. That is why, the size of array is not the original one.
Let us see one example of array decay using C++ code,
Example
#include<iostream> using namespace std; void DisplayValue(int *p) { cout << "New size of array by passing the value : "; cout << sizeof(p) << endl; } void DisplayPointer(int (*p)[10]) { cout << "New size of array by passing the pointer : "; cout << sizeof(p) << endl; } int main() { int arr[10] = {1, 2, }; cout << "Actual size of array is : "; cout << sizeof(arr) <<endl; DisplayValue(arr); DisplayPointer(&arr); }
Output
Actual size of array is : 40 New size of array by passing the value : 8 New size of array by passing the pointer : 8
Now, we will see how to prevent the array decay in C++. There are two following ways to prevent the array decay.
- Array decay is prevented by passing the size of array as a parameter and do not use sizeof() on parameters of array.
- Pass the array into the function by reference. It prevents the conversion of array into pointer and it prevents array decay.
Example
#include<iostream> using namespace std; void Display(int (&p)[10]) { cout << "New size of array by passing reference: "; cout << sizeof(p) << endl; } int main() { int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout << "Actual size of array is: "; cout << sizeof(arr) <<endl; Display(arr); }
Output
Actual size of array is: 40 New size of array by passing reference: 40
- Related Articles
- What is Array Decay in C++?
- What is reverberation? How can it be reduced?
- (a) Explain the pH change as the cause of tooth decay. How can tooth decay caused by pH change be prevented?(b) Explain how pH change in the lake water can endanger the lives of aquatic animals (like fish). What can be done to lessen the danger to the lives of aquatic animals in the lake?
- Name one disease that can be prevented by vaccination.
- (a) Describe the parts of our tooth with the help of a labelled diagram.(b) What is meant by dental caries? How are they caused?(c) What is dental plaque? What harm can it do? How can the formation of plaque be prevented?
- Why does the medium become acidic in mouth? What is the ill effect of this acidic medium? How can this be prevented?
- What is Sieving? Where can it be used?
- What is the cause of diabetes? How it can be controlled?
- What is non-enumerable property in JavaScript and how can it be created?
- How soil pollution and soil erosion could be prevented?
- What is SciPy in Python? Explain how it can be installed, and its applications?
- What is hysteresis thresholding? How can it be achieved using scikit-learn in Python?
- Explain how soil pollution and soil erosion could be prevented.
- What is no-confidence motion? When can it be held?
- What is the meaning of “SELECT” statement in MySQL and how can it be used?

Advertisements