Difference between new and malloc in C++

In this blog, we will explore two essential memory allocation features in C++: ‘new’ and ‘malloc’. Dynamic memory allocation plays a crucial role in programming and requires developers to consider it carefully. Developers often face multiple solutions for a given problem, requiring us to select the most optimal approach. Achieving optimization relies on efficient memory usage, aiming to minimize unnecessary memory consumption and avoid wastage. Let’s delve into the fundamentals of ‘new’ and ‘malloc’ and differentiate between these two memory allocation techniques.

Understanding memory allocation in C++: ‘malloc’ vs ‘new’

Malloc:

malloc‘ functions are inherited from C and are used to allocate a block of memory of a specified size. Here are some properties of malloc:

  • malloc does not call any constructor.
  • It only allocates memory but does not initialize it.
  • Explicit casting is required to assign a return value to a pointer of any specific type because the return value of malloc is void.

Example:

The following code demonstrates a common pattern for dynamic memory allocation, especially when working with a single object using malloc :

int* m = (int*)malloc(sizeof(int));

The following example illustrates a common pattern for dynamically allocating memory, specifically for an array of 10 integers.

int* arr = (int*)malloc(10 * sizeof(int));

From the two examples above, we can grasp the common syntax typically used with malloc.

New:

The ‘new‘ operator is specific to C++ and is used to allocate memory for objects dynamically. Moreover, it not only allocates memory but also initializes the object by calling its constructor.

Here are some properties of the new:

  • new is type-safe and is aware of the object type it is allocating memory for.
  • It automatically handles memory allocation and initialization, making code cleaner and less error-prone.
  • Memory allocated with it should be deallocated using the delete operator to avoid memory leaks and to reduce the waste of memory.
int* pt = new int(9);

The above example allocates the memory for an integer initialized to 9, with the use of the new operator.

Difference Between them:

  1. Initialization:
    • Malloc: It merely allocates memory blocks of specified sizes without initializing them.
    • New: Unlike malloc, ‘new’ not only allocates memory, but also initializes the object by calling its constructor.
  2. Type Handling:
    • Malloc: It requires explicit casting of the returned void pointer to the desired data type, making it less type-safe.
    • New: The ‘new’ operator, recognizing the object type it allocates memory for, guarantees type safety, removing the necessity for explicit casting.
  3. Error-Prone Nature:
    • Malloc: Due to the lack of automatic initialization and type safety, manual handling increases the number of errors.
    • New: With automatic memory allocation and initialization, ‘new’ reduces errors and enhances code clarity.
  4. Deallocation:
    • Malloc: Memory allocated with malloc should be explicitly deallocated using ‘free’ to prevent memory leaks.
    • New: Memory allocated with ‘new’ should be deallocated using the ‘delete’ operator to avoid memory leaks.
  5. Syntax:
    • Malloc: The syntax involves using malloc along with explicit casting and specifying the size of memory required.
    • New: The syntax of ‘new’ is simpler and more intuitive, directly initializing objects without the need for casting.

Leave a Comment

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

Scroll to Top