Dynamic memory allocation in C++:
“New” is an operator in c++. Which is used to allocate the memory dynamically.
In C, dynamically memory allocation:
If malloc is success, void pointer is returned.
If malloc fails then NULL pointer is returned.
If (n*sizeof(int)) is success, then only type casting allowed.
In C++, dynamic memory allocation:
What are the advantages of ‘new’ operator in C++ for dynamic memory allocation?
Exception Handling: while running programs, some exceptions(errors) occurs during memory allocation. In C, we can’t able to identify the particular error with proper error message.
But in C++, ‘new’ is an operator. It will give proper message to programmer. Then user can take required action over there.
If allocation is success, then integer pointer is returned. If an error occurs then, ‘new, will through an exception rather than NULL pointer.
Malloc returns NULL pointer on failure. Which doesn’t give proper exception handling message. This is one of the advantage of ‘new’ compared to malloc().
‘New’ is an operator, which can be over loadable. ‘new’ has enhanced version, that means we can have privileges to design new memory allocation based on user requirement. User can be able to define memory allocation function(user defined memory allocation function) with the help of ‘new’.
But, by using malloc predefined library functions, only we can able to create memory. User defined privileges are not given for creation of malloc().
In C, free() is a keyword to free or de-allocate memory after usage. The freed memory will be utilized by remaining part of program while at runtime. In C++, delete() is a keyword which de-allocates the memory which has been allocated by ‘new’ operator only.
Syntax:
To delete(free) the memory in C++,
Example:
“New” is an operator in c++. Which is used to allocate the memory dynamically.
In C, dynamically memory allocation:
Int *p; P=malloc(n*sizeof(int));
Here malloc is a library function.
If malloc is success, void pointer is returned.
If malloc fails then NULL pointer is returned.
If (n*sizeof(int)) is success, then only type casting allowed.
In C++, dynamic memory allocation:
Int *p=new int[5]; Data_type *pv; pv=new Data_type[size];
Here we are using integer array. Hence return type is integer pointer.
What are the advantages of ‘new’ operator in C++ for dynamic memory allocation?
Exception Handling: while running programs, some exceptions(errors) occurs during memory allocation. In C, we can’t able to identify the particular error with proper error message.
But in C++, ‘new’ is an operator. It will give proper message to programmer. Then user can take required action over there.
If allocation is success, then integer pointer is returned. If an error occurs then, ‘new, will through an exception rather than NULL pointer.
Malloc returns NULL pointer on failure. Which doesn’t give proper exception handling message. This is one of the advantage of ‘new’ compared to malloc().
‘New’ is an operator, which can be over loadable. ‘new’ has enhanced version, that means we can have privileges to design new memory allocation based on user requirement. User can be able to define memory allocation function(user defined memory allocation function) with the help of ‘new’.
But, by using malloc predefined library functions, only we can able to create memory. User defined privileges are not given for creation of malloc().
In C, free() is a keyword to free or de-allocate memory after usage. The freed memory will be utilized by remaining part of program while at runtime. In C++, delete() is a keyword which de-allocates the memory which has been allocated by ‘new’ operator only.
Syntax:
To delete(free) the memory in C++,
int *p=new int[5]; //to create memory for 5 integers float *fp=new float[5]; double *dp=new double[5];
Data_type *pv=new data_type[size]; Here the data type may be a class or object.
Example:
Height **p=new Height(); int *p=new int(10); // to store integer 10 into address of pointer p Height *my_height=new Height[3]; // here 3 objects created Height *myh= new Height(10); //Here only, one object is initialized. int *p=new int[5]; delete p; //to delete or free variable memory delete []p; //to delete array
What is dangling pointer problem in C++
int *p=new int(10); …………. …………. delete p; //here we are deleting memory location …………….. …………….. p=new int(50); //here we are trying to access deleted memory. This is called
//dangling pointer problemint *p=new int(10); ………….. …………. p=new int(50); //memory leak occurs here. Because we are trying to replace //existing variable with new one. ……. ……………….Example:class Bag { int item_count; int contents[5]; }This is for fixed number of objects