C++ new vs malloc with Example

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++:

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:

Output:

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++:

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:

Output:

new vs malloc

NewMalloc
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.

Leave a Comment

Your email address will not be published. Required fields are marked *