Pointers and References: Returning Dynamically Allocated Memory from a Pointer Function
What is it?
Returning dynamically allocated memory from a function in C++ is a technique which involves the use of pointers. A function creates a dynamic memory location (using new
keyword), stores some value in it and then returns the address of that memory location. This address is returned as a pointer.
Syntax
The syntax of creating and returning dynamically allocated memory is:
TypeName* functionName() {
TypeName* pointerName = new TypeName;
// Do something with pointerName
return pointerName;
}
Where,
TypeName
: It is the data type of the value which is stored in the dynamically allocated memory.functionName
: It is the name of the function which returns the dynamically allocated memory.pointerName
: It is the pointer which holds the address of the dynamically allocated memory.
How to use it
To return dynamically allocated memory from a function, follow the below steps:
- Create a function that returns a pointer to a specific type.
- Inside this function, dynamically allocate memory using the
new
keyword. - Perform operations on this memory if needed.
- Finally, return the pointer that points to this dynamically allocated memory.
Program code snippet example
#include <iostream>
using namespace std;
int* createAndInitialize() {
// Dynamically allocate memory for an integer
int* num = new int;
// Initialize the dynamically allocated integer with a value
*num = 25;
// Return the pointer to the dynamically allocated memory
return num;
}
int main() {
// Call the function and store the returned pointer
int* myNum = createAndInitialize();
cout << "Value: " << *myNum << endl;
// Don't forget to delete the dynamically allocated memory!
delete myNum;
return 0;
}
Output:
Value: 25
Important to know
- Returning dynamically allocated memory can be useful when the size of the data is not known at compile time or if the data size is too large to allocate on the stack.
- The returned pointer should always be deleted after use to avoid memory leaks.
- Always check if the dynamically allocated memory is successfully allocated. If
new
fails, it will return anullptr
.
Best practices
When returning dynamically allocated memory, it is important to keep the following best practices in mind:
Always deallocate memory: The function that allocates the memory should not be the one to deallocate it. Responsibility for deallocation should be given to the function that receives the dynamically allocated memory.
Check for successful allocation: Always check if the memory allocation was successful. If the memory allocation fails,
new
will throw abad_alloc
exception.Avoid returning local objects: Don't return pointers to local objects. Local objects get destroyed once the function execution is complete, and trying to access them can lead to undefined behavior.
With the given create_array
function with the size
and init_value
parameter, return the pointer to the allocated memory of the correct array size and initial values.