What is new?
New is a keyword in c++ and it is a type of memory allocator which is responsible for allocating memory in a dynamic way. The heap memory is allocated by this operator and it shares the beginning address of the memory assigned to a certain variable.
The working of the new keyword is similar to malloc and both of the keywords can be used with C++.
Although we prefer using new keyword because it possess several advantages over malloc.
Here’s a way to represent new keyword in C++:
data_type variable_name_decided = new data_type ( arguments_passed ) ;
In the above syntax “data_type” refers to the datatype for which you want to create memory space in a dynamic way, “variable_name_decided” is used to represent the variable of a particular datatype, “new” keyword is used to allocate memory dynamically, “arguments_passed” can be arguments passed in the constructor for initializing the constructed object.
We are aware that the heap’s capacity is constrained and that the new operator allocates memory to it. As a result, the new operator will fail if the heap runs out of memory and it tries to allocate memory. The program terminates unexpectedly if your code is unable to handle an exception that the new operator throws if it is unable to allocate the memory.
Code:
#include<iostream> using namespace std; int main() { int *myptr ; myptr = new int ; cout << "Please enter your age : " << " \n "; cin >>*myptr; cout << "Your age is: " << *myptr << " \n "; return 0; }
Output:
Please enter your age : 55 Your age is: 55
What is malloc?
malloc is a method or function used for allocating the desired quantity of memory in heap. The malloc method is always responsible for returning void as its return type which is later type-cased for providing pointer to memory of desired type. Since malloc() is used to create memory dynamically, it is comparable to the new operator in C++. It is basically a standard library function.
Here is a way to use malloc method in C++:
data_type variable_name_decided = ( data_type * ) malloc ( sizeof ( data_type ) ) ;
In the above syntax “ data_type ” refers as the datatype for which you want to create memory space in a dynamic way, “ variable_name_decided ” is used to represent the variable of a particular datatype, “ data_type * ” is used to typecast to a pointer, “ sizeof ” is used to get the memory size required.
Code:
#include<iostream> using namespace std; int main() { int size; cout << "Please enter the number of people : " << " \n "; cin >> size; int *myptr; myptr = ( int* ) malloc( sizeof(int)*size ); for( int i=0; i<size; i++) { cout << "Please type the age of " << (i+1) << " Person: " <<"\n"; cin >> *(myptr+i); } cout << "The Age of all the persons are printed below : " <<"\n"; for( int i=0; i<size; i++) { cout << *(myptr+i) << "\n"; } return 0; }
Output:
Please enter the number of people : 3 Please type the age of 1 Person: 34 Please type the age of 2 Person: 54 Please type the age of 3 Person: 22 The Age of all the persons are printed below : 34 54 22
new vs malloc
New | Malloc |
New operator can be called in different programming langugages including C#, C++, and Java. | Malloc method is particularly a feature of C programming language, although it can be used in C++ and some other languages as well. |
New operator can be used to call certain constructors of a particular object. | Malloc method can never be used to call constructor of any object. |
New operator gives the return type which is same as the data type. | Malloc method always return void*. |
New operator doesn’t contain any feature to reallocate the memory. | We can use realloc() method to reallocate memory that was earlier allocated by malloc(). |
New operator throws an exception if it fails in execution. | Malloc method returns NULL if it fails in execution. |
Delete method can be used to deallocate the memory allocated by new operator. | Free method can be used to deallocate the memory allocated by malloc method. |
Conclusion
In the modern days, new operator is majorly used for allocating the memory as it possesses some advantages over malloc method. Malloc was the old method so it was used before the introduction of new operator.
I hope you were able to understand the difference between new and malloc clearly. Feel free to comment below if you are still facing any difficulties.
The post C++ new vs malloc with Example appeared first on The Crazy Programmer.
from The Crazy Programmer https://ift.tt/97as6ix
Post a Comment