
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why C++ Doesn't Support Functions Returning Arrays
C++ does not support functions that return arrays directly because arrays do not have a built-in size or type information that can be returned along with the array. This design choice was made by the C++ creators to avoid memory management issues. But, we have alternative methods to achieve similar result.
In this article, we will understand why C++ does not support functions returning arrays and how to overcome this limitation using other techniques.
Functions Returning an Array
First of all, let's understand what will happen if we try to return an array from a function in C++. Array are not a first-class type, it is a combination of a data type and a size. Means, an integer array is not just an int type, but a int type with a specific size ( like int[10] ). So, when you try to return an array from a function, the compiler does not know how to handle the size of the array.
Hence, it will return the address of the first element of the array (ie, a pointer), which defined inside the function. But, the scope of the array is only within the function. Means, you will get a out of scope error when you are returning an array from a function. Now, Let's see an example code.
Example
#include <iostream> using namespace std; int* functionArray() { int a[10]; a[0] = 7; a[1] = 6; return a; } int main() { int* p = functionArray(); cout << p[0] << " " << p[1]; return 0; }
The output of the above code will be an error message similar to the following:
Warnings/Errors: main.cpp: In function 'int* functionArray()': main.cpp:9:11: warning: address of local variable 'a' returned [-Wreturn-local-addr] 9 | return a;
Why C++ Does Not Allow Functions to Return Arrays?
C++ does not allow functions to return arrays directly for the following reasons:
- Can't Copy Arrays: Arrays do not support assignment operation(=), which means you cannot copy an array to another array to return it from a function.
- No Size Information: An independent array does not carry size information with it. So, when you return an array pointer, the compiler does not know how many elements are in the array.
- Memory Management Issues: Returning an array from a function can cause issues in memory management. If the array is allocated on the stack, it will be deallocated when the function returns, leading to dangling pointers.
Alternative Methods for Returning Arrays From Function
There are some indirect ways to return arrays from functions in C++. Here we will discuss some of the common methods:
1. Using std::array
The std::array is a fixed-size array defined in the C++ STL Library. This will let you declare an array with a specified size and return it from a function. Hence, the size of the array is known at compile time, so functions can return std::array objects without any issues. Here is an example:
#include <iostream> #include <array> using namespace std; array<int, 5> functionArray() { std::array<int, 5> arr = {1, 2, 3, 4, 5}; return arr; } int main() { array<int, 5> arr = functionArray(); for (int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; } return 0; }
The output of the above code will be:
1 2 3 4 5
2. Using std::vector
If you want to return a dynamic array from a function, you can use std::vector. Vectors are dynamic arrays that can grow and shrink in size at runtime. Even though size of the vector is not known at compile time, C++ allows you to return it from functions. Because, vectors are objects that manage their own memory and can be copied using assignment operator. Here is an example:
#include <iostream> #include <vector> using namespace std; vector<int> functionVector() { vector<int> vec = {1, 2, 3, 4, 5}; return vec; } int main() { vector<int> vec = functionVector(); for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } return 0; }
The output of the above code will be:
1 2 3 4 5
3. Using Dynamically Allocated Arrays
Dynamically allocated arrays are the arrays that are defined using the new operator. From a function, you can return a pointer to a dynamically allocated array. But, in this case you have to manually manage the memory using delete[] operator to avoid memory leaks. Here is an example:
#include <iostream> using namespace std; int* functionArray(int size) { // Dynamically allocate array int* arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = i * 2; } // Return pointer to the array return arr; } int main() { int size = 5; int* arr = functionArray(size); for (int i = 0; i < size; i++) { cout << arr[i] << " "; // Print the array values } // Deallocate memory delete[] arr; return 0; }
The output of the above code will be:
0 2 4 6 8