Difference between revisions of "Dynamic Memory Allocation in C++"
Proj user18 (talk | contribs) (→Destructor) |
Proj user18 (talk | contribs) (→Dynamic Memory Allocation) |
||
Line 1: | Line 1: | ||
+ | |||
= Dynamic Memory Allocation = | = Dynamic Memory Allocation = | ||
Revision as of 05:00, 18 December 2016
Contents
Dynamic Memory Allocation
Certain memory requirements of a program can be determined before program execution. In such a case, variables can be declared appropriately. However, there are cases when the memory requirement can be known only during run-time. In such cases, dynamic memory allocation is required. The lifetime of the objects created using dynamic memory allocation is not limited by the scope in which they were created. Dynamic memory is allocated in the heap.
Dynamic memory allocation and deallocation in C++ is achieved using operators new and delete.
new
new allocates memory as requested and returns a pointer to the beginning of the block of memory allocated.
Syntax:
type *ptr = new type; type *ptr = new type[n];
In the first expression, memory is allocated for a single element and a pointer to the memory location is returned.
Example: int *data= new int;
In the second expression, memory is allocated for an array of ‘n’ elements of type type.
Example: int *data = new int[5];<br>
A block of memory for 5 integer data is allocated and a pointer to the start of the memory block is returned.
delete
delete operator is invoked to explicitly release the dynamically allocated memory.
Syntax:
delete ptr; delete[] ptr;
In the first expression, memory allocated to a single element is released.
Example: delete data;
In the second expression, memory allocated to an array of elements is deleted.
Example: delete[] data;
new and delete internally use constructors and destructors to achieve dynamic memory allocation.
Dynamic allocation of objects
Constructor
A constructor is a special member function of the class which is invoked when we create new instances of the class.
Consider a class TestClass:
class TestClass
{
public:
TestClass();
TestClass(int length);
TestClass(const TestClass& other);
~TestClass();
int get_num_data() const;
private:
int *data;
int num_data;
};
We can create objects using constructors in the following way:
TestClass ObjectOfTestClass(10);
Constructors are extremely useful as they allow us to assign initial values to member variables.
There are three types of Constructors:
Default constructor
Default constructor creates instances of a class without the need of an actual parameter list.
Example:
TestClass::TestClass() : data(nullptr), num_data(0)
{
}
Parameterized Constructor
Parameterized constructor accepts parameters which can be assigned as initial values to the member variables.
Example:
TestClass::TestClass(int length) : data(nullptr), num_data(length)
{
}
Copy Constructor
Copy constructor is invoked when an object is initialized using another object of the same type.
Example:
TestClass::TestClass(const TestClass& anotherObject)
{
num_data = anotherObject.num_data;
data = new int[num_data];
for(int i=0; i<num_data; i++)
{
data[i] = anotherObject.data[i];
}
}
Destructor
A destructor is a special member function called when the object is no longer in use. The memory occupied will be freed.
Example:
TestClass::~TestClass()
{
if(data != nullptr)
{
delete [] data;
}
}
In C++, default constructors and default destructors are automatically invoked.
Also, C++ has a provision for default Copy constructor. However, this is not always tailored to our requirements. Hence, it is a good practice to define our own copy constructor.
Memory Leak
Memory Leaks occur when memory is dynamically allocated but never deallocated. Memory leak accumulates over time and the system may run out of memory. Hence, caution must be exercised while allocating memory dynamically.