Dynamic Variables
Dynamic variables in C++ refer to variables whose memory is allocated at runtime rather than compile-time. This allows for flexible memory management, enabling the creation of data structures like dynamic arrays, linked lists, and other complex structures that require variable sizes. Dynamic memory allocation is achieved using the new and deleted operators.
Dynamic Memory Allocation
Allocating Memory
Use the new operator to allocate memory dynamically. It allocates memory from the heap and returns a pointer to the beginning of the block.
Allocating a Single Variable
Example:
#include <iostream>
int main() {
int* ptr = new int; // dynamically allocate memory for an int
*ptr = 10; // assign a value to the allocated memory
std::cout << "Value: " << *ptr << std::endl;
delete ptr; // free the allocated memory
return 0;
}
Allocating an Array
Example:
#include <iostream>
int main() {
int size = 5;
int* arr = new int[size]; // dynamically allocate memory for an array of integers
for (int i = 0; i < size; i++) {
arr[i] = i * 10;
}
for (int i = 0; i < size; i++) {
std::cout << "Element " << i << ": " << arr[i] << std::endl;
}
delete[] arr; // free the allocated memory for the array
return 0;
}
Dynamic Memory Deallocation
Memory allocated with new must be deallocated using the delete operator to avoid memory leaks.
Deleting a Single Variable
Example:
#include <iostream>
int main() {
int* ptr = new int(10); // dynamically allocate and initialize memory for an int
std::cout << "Value: " << *ptr << std::endl;
delete ptr; // free the allocated memory
ptr = nullptr; // set pointer to null to avoid dangling pointer
return 0;
}
Deleting an Array
Example:
#include <iostream>
int main() {
int size = 5;
int* arr = new int[size]; // dynamically allocate memory for an array of integers
for (int i = 0; i < size; i++) {
arr[i] = i * 10;
}
for (int i = 0; i < size; i++) {
std::cout << "Element " << i << ": " << arr[i] << std::endl;
}
delete[] arr; // free the allocated memory for the array
arr = nullptr; // set pointer to null to avoid dangling pointer
return 0;
}
Using Smart Pointers
In modern C++, using smart pointers provided by the C++ Standard Library to manage dynamic memory is recommended. Smart pointers automatically manage the lifetime of dynamically allocated objects and help prevent memory leaks and dangling pointers.
std::unique_ptr
std::unique_ptr ensures that a resource is owned by one and only one pointer at a time.
Example:
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> ptr = std::make_unique<int>(10); // allocate and initialize memory
std::cout << "Value: " << *ptr << std::endl;
// no need to delete, std::unique_ptr automatically deallocates memory
return 0;
}
std::shared_ptr
std::shared_ptr allows multiple pointers to share ownership of a resource. The resource is destroyed when the last std::shared_ptr owning it is destroyed.
Example:
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<int> ptr1 = std::make_shared<int>(20); // allocate and initialize memory
std::shared_ptr<int> ptr2 = ptr1; // share ownership
std::cout << "Value: " << *ptr1 << std::endl;
std::cout << "Value: " << *ptr2 << std::endl;
// no need to delete, std::shared_ptr automatically deallocates memory
return 0;
}
Example Code Demonstrating Dynamic Variables
Here's a complete example demonstrating dynamic memory allocation for single variables, arrays, and using intelligent pointers:
#include <iostream>
#include <memory>
void dynamicSingleVariable() {
int* ptr = new int(10); // dynamically allocate and initialize memory
std::cout << "Dynamic single variable value: " << *ptr << std::endl;
delete ptr; // free the allocated memory
ptr = nullptr;
}
void dynamicArray() {
int size = 5;
int* arr = new int[size]; // dynamically allocate memory for an array
for (int i = 0; i < size; i++) {
arr[i] = i * 10;
}
std::cout << "Dynamic array values:" << std::endl;
for (int i = 0; i < size; i++) {
std::cout << "Element " << i << ": " << arr[i] << std::endl;
}
delete[] arr; // free the allocated memory for the array
arr = nullptr;
}
void smartPointerExample() {
std::unique_ptr<int> uptr = std::make_unique<int>(30); // allocate and initialize memory
std::cout << "Smart pointer value: " << *uptr << std::endl;
std::shared_ptr<int> sptr1 = std::make_shared<int>(40); // allocate and initialize memory
std::shared_ptr<int> sptr2 = sptr1; // share ownership
std::cout << "Shared pointer value: " << *sptr1 << std::endl;
std::cout << "Shared pointer value: " << *sptr2 << std::endl;
}
int main() {
dynamicSingleVariable();
std::cout << std::endl;
dynamicArray();
std::cout << std::endl;
smartPointerExample();
return 0;
}
This example demonstrates the basics of dynamic memory allocation, deallocation, and the use of smart pointers to manage dynamic memory effectively.