Difference between revisions of "Dynamic Memory Allocation in C++"
Proj user18 (talk | contribs) (Created page with "== Dynamic Memory Allocation ==") |
Proj user18 (talk | contribs) (→Dynamic Memory Allocation) |
||
Line 1: | Line 1: | ||
− | + | = 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 allocation and deallocation in C++ is achieved using operators new and delete.<br> | ||
+ | |||
+ | ==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]''; | ||
+ | <br> | ||
+ | In the first expression, memory is allocated for a single element and a pointer to the memory location is returned.<br> | ||
+ | <syntaxhighlight lang="c"> | ||
+ | Example: int *data= new int; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | In the second expression, memory is allocated for an array of ''‘n’'' elements of type ''type''.<br> | ||
+ | <syntaxhighlight lang="c"> | ||
+ | Example: int *data = new int[5];<br> | ||
+ | </syntaxhighlight> | ||
+ | A block of memory for 5 integer data is allocated and a pointer to the start of the memory block is returned. | ||
+ | <br> | ||
+ | |||
+ | ==delete== | ||
+ | delete operator is invoked to explicitly release the dynamically allocated memory. | ||
+ | |||
+ | '''Syntax:''' | ||
+ | delete ptr; | ||
+ | delete[] ptr; | ||
+ | <br> | ||
+ | In the first expression, memory allocated to a single element is released.<br> | ||
+ | Example: '''delete data;'''<br> | ||
+ | In the second expression, memory allocated to an array of elements is deleted. <br> | ||
+ | Example: '''delete[] data;''' | ||
+ | |||
+ | <br> | ||
+ | new and delete internally use constructors and destructors to achieve dynamic memory allocation. | ||
+ | |||
+ | = Dynamic allocation of objects = | ||
+ | |||
+ | == Constructors == | ||
+ | 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:<br> | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | class TestClass | ||
+ | { | ||
+ | public: | ||
+ | TestClass(); | ||
+ | TestClass(int length); | ||
+ | TestClass(const TestClass& other); | ||
+ | ~TestClass(); | ||
+ | |||
+ | int get_num_data() const; | ||
+ | |||
+ | private: | ||
+ | int *data; | ||
+ | int num_data; | ||
+ | }; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | We can create objects using constructors in the following way: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | TestClass ObjectOfTestClass(10); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | Constructors are extremely useful as they allow us to assign initial values to member variables.<br> | ||
+ | |||
+ | There are three types of Constructors: | ||
+ | |||
+ | === Default constructor === | ||
+ | Default constructor creates instances of a class without the need of an actual parameter list. | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | Example: | ||
+ | TestClass::TestClass() : data(nullptr), num_data(0) | ||
+ | { | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | === Parameterized Constructor === | ||
+ | Parameterized constructor accepts parameters which can be assigned as initial values to the member variables. | ||
+ | <syntaxhighlight lang="c"> | ||
+ | Example: | ||
+ | TestClass::TestClass(int length) : data(nullptr), num_data(length) | ||
+ | { | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | === |
Revision as of 04:42, 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 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
Constructors
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)
{
}