Tutorials
C++The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant.
In programming there may be scenarios where programmers may not know the memory needed until run time. In this case, the programmer can opt to reserve as much memory as possible, assigning the maximum memory space needed to tackle this situation. This would result in wastage of unused memory spaces. Memory management operators are used to handle this situation in C++ programming language
There are two types of memory management operators in C++:
These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways.
The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object of any type.
General syntax of new operator in C++:
The general syntax of new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable a holds the address of memory space allocated.
Dynamic variables are never initialized by the compiler. Therefore, the programmer should make it a practice to first assign them a value.
The assignment can be made in either of the two ways:
|
The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;
In the above example, delete is a keyword and the pointer variable is the pointer that points to the objects already created in the new operator. Some of the important points the programmer must note while using memory management operators are described below:
Example: to understand the concept of new and delete memory management operator in C++:
|
The output of the above program is
The Output is:a=100
In the above program, the statement:
int *a= new a;
Holds memory space in memory for storing a integer datatype. The statement:
*a=100
This denotes that the value present in address location pointed by the pointer variable a is 100 and this value of a is printed in the output statement giving the output shown in the example above. The memory allocated by the new operator for storing the integer variable pointed by a is released using the delete operator as:
delete a;
|
int *a= new a; should be int *a = new int; The new operator accepts types, not variable names. |
|
Reading what it is exposed before that new is the statement that allocates memory blocks, makes me wonder about all the other situations if you should worry about releasing the memory used for example: If you have a subroutine like this int subroutine(int param){ char example_variable [200]; whatever the routine does.... } is necessary to release the memory used by the variable example_variable??? or is this automatically released when the variable get out of scope??? I guess that what i'm trying to ask is: do i only have to worry for the objects that i create using the new statement??? AR |
|
After reading this Article, there is some situations that makes me wonder how's is a correct memory management. Let's say in a routine inside your code, you have some variables such as this; int routine(int param){ char example[200]; ...}do i have to worry about the liberation of the memory used by the variable example? or it is released automatically once the variable get out of scope?i guess what i'm trying to ask is , the only memory i have to worry to release is the one used by the variables created using the new statement? |
| plz send show the veriable address in c++ |
|
variables are created in different spaces within a program. the example[100] variable would be created within a stack which would be deleted as soon as the program terminates so the programmer doesn't need to take care of freeing that. but any address dynamically obtained is obtained from the heap which is a pool of memory of all the programs running and not specific to one. so a programmer needs to take care of freeing or deleting (basically returning the space back to the pool) when the purpose of that variable is served and no longer required. |
| what does the delete operator do in addition to deallocation of memory used by object |