Difference Wiki

New vs. Malloc( ): What's the Difference?

Edited by Huma Saeed || By Sumera Saeed || Published on February 1, 2024
New initializes objects, calling constructors in C++; Malloc() allocates memory without initialization in C.

Key Differences

New in C++ not only allocates memory but also initializes the object by calling the constructor. It returns a fully typed pointer, ensuring type safety. For instance, new int allocates memory for an integer and initializes it. Malloc(), a C function, allocates a specified amount of memory and returns a void pointer to it. This pointer can then be cast to any desired type. Malloc() does not initialize the memory; it leaves it with indeterminate content.
Huma Saeed
Feb 01, 2024
New throws an exception when memory allocation fails, providing a robust way to handle memory allocation errors. It's part of C++'s standard exception handling mechanism. Malloc(), when failing to allocate memory, returns a null pointer. This requires manual error checking after each call to Malloc(), a practice that is error-prone and often overlooked.
Sumera Saeed
Feb 01, 2024
New can be overloaded and customized in C++, allowing developers to define their own behavior for memory allocation and object initialization. Malloc() is a library function and cannot be overloaded. It follows a fixed behavior defined by the C standard library, providing a simple, straightforward way to allocate memory without any frills.
Sumera Saeed
Feb 01, 2024

Comparison Chart

Language

C++
C
Sumera Saeed
Feb 01, 2024

Initialization

Allocates and initializes
Allocates without initialization
Sumera Saeed
Feb 01, 2024
ADVERTISEMENT

Error Handling

Throws an exception on failure
Returns NULL on failure
Sumera Saeed
Feb 01, 2024

Return Type

Typed pointer
Void pointer (requires casting)
Sumera Saeed
Feb 01, 2024

Customization

Can be overloaded
Cannot be overloaded
Janet White
Feb 01, 2024

Memory Handling

Tied to object lifecycles
Pure memory allocation
Sumera Saeed
Feb 01, 2024

Constructors

Calls constructors
Does not call constructors
Sumera Saeed
Feb 01, 2024
ADVERTISEMENT

New and Malloc( ) Definitions

New

Can be customized in class definitions.
Void* MyClass::operator new(size_t size) { /* custom allocation */ }
Sumera Saeed
Jan 23, 2024

Malloc( )

Allocates a block of memory without initializing.
Int* p = (int*)malloc(sizeof(int));
Sumera Saeed
Jan 23, 2024

New

Allocates and initializes objects.
Int* p = new int(5);
Sumera Saeed
Jan 23, 2024

Malloc( )

Returns NULL if memory cannot be allocated.
Int* p = (int*)malloc(sizeof(int)); if (!p) { /* handle error */ }
Janet White
Jan 23, 2024

New

Allocates memory for a specific type.
Double* p = new double;
Sumera Saeed
Jan 23, 2024

Malloc( )

Requires the size of memory to allocate.
Float* array = (float*)malloc(10 * sizeof(float));
Janet White
Jan 23, 2024

New

Automatically calls the constructor of the class.
MyClass* obj = new MyClass();
Huma Saeed
Jan 23, 2024

Malloc( )

Part of the C standard library.
Void* memory = malloc(1024);
Harlon Moss
Jan 23, 2024

New

Throws a bad_alloc exception if memory allocation fails.
Try { int* p = new int; } catch (std::bad_alloc& e) { /* handle error */ }
Sumera Saeed
Jan 23, 2024

Malloc( )

Returns a void pointer, requiring type casting.
Char* str = (char*)malloc(50 * sizeof(char));
Sumera Saeed
Jan 23, 2024

Malloc( )

(computing) A subroutine in the C programming language's standard library for performing dynamic memory allocation.
Sumera Saeed
Jan 22, 2024

Malloc( )

(computing) To allocate memory using the C programming language malloc subroutine.
Sumera Saeed
Jan 22, 2024

FAQs

How does 'new' differ from traditional memory allocation?

'new' not only allocates memory but also initializes the memory by calling the object's constructor.
Huma Saeed
Feb 01, 2024

Can you give a basic example of using 'new'?

int* ptr = new int; allocates memory for an integer.
Sumera Saeed
Feb 01, 2024

What is 'new' in C++?

'new' is a C++ operator used for dynamic memory allocation.
Sumera Saeed
Feb 01, 2024

What happens if 'new' fails in C++?

If 'new' fails, it throws a bad_alloc exception.
Sumera Saeed
Feb 01, 2024

Is 'new' a function or an operator?

'new' is an operator in C++.
Aimie Carlson
Feb 01, 2024

Can 'new' be used for array allocation?

Yes, e.g., int* arr = new int[10]; allocates an array of 10 integers.
Sumera Saeed
Feb 01, 2024

Does 'new' return a pointer?

Yes, 'new' returns a pointer to the allocated memory.
Aimie Carlson
Feb 01, 2024

What is placement 'new'?

Placement 'new' allows allocating memory at a specified location.
Janet White
Feb 01, 2024

How does 'malloc()' work?

'malloc()' allocates a specified number of bytes and returns a pointer to the allocated memory.
Janet White
Feb 01, 2024

What is 'malloc()' in C?

'malloc()' is a function in C used for dynamic memory allocation.
Harlon Moss
Feb 01, 2024

What if 'malloc()' fails to allocate memory?

If 'malloc()' fails, it returns a NULL pointer.
Harlon Moss
Feb 01, 2024

Does 'malloc()' initialize the allocated memory?

No, 'malloc()' does not initialize the memory; it leaves it uninitialized.
Harlon Moss
Feb 01, 2024

How do you deallocate memory allocated by 'new'?

Use delete to deallocate memory allocated by 'new'.
Janet White
Feb 01, 2024

Can 'new' initialize an object?

Yes, 'new' can initialize objects using constructors.
Harlon Moss
Feb 01, 2024

What header file is required for 'malloc()'?

The stdlib.h header file is required for 'malloc()'.
Harlon Moss
Feb 01, 2024

Can you provide a basic example of 'malloc()'?

int* ptr = (int*)malloc(sizeof(int)); allocates memory for an integer.
Sumera Saeed
Feb 01, 2024

How do you free memory allocated by 'malloc()'?

Use free() to deallocate memory allocated by 'malloc()'.
Janet White
Feb 01, 2024

Can 'malloc()' be used for arrays?

Yes, e.g., int* arr = (int*)malloc(10 * sizeof(int)); allocates an array of 10 integers.
Harlon Moss
Feb 01, 2024

How do you determine the size for 'malloc()' allocation?

The size is determined based on the size of the data type and the number of elements, calculated using sizeof().
Aimie Carlson
Feb 01, 2024

Is 'malloc()' specific to C?

'malloc()' is primarily used in C, though it can also be used in C++.
Harlon Moss
Feb 01, 2024
About Author
Written by
Sumera Saeed
Sumera is an experienced content writer and editor with a niche in comparative analysis. At Diffeence Wiki, she crafts clear and unbiased comparisons to guide readers in making informed decisions. With a dedication to thorough research and quality, Sumera's work stands out in the digital realm. Off the clock, she enjoys reading and exploring diverse cultures.
Edited by
Huma Saeed
Huma is a renowned researcher acclaimed for her innovative work in Difference Wiki. Her dedication has led to key breakthroughs, establishing her prominence in academia. Her contributions continually inspire and guide her field.

Trending Comparisons

Popular Comparisons

New Comparisons