Introduction to C++ | malloc vs new
C++ malloc vs new
In C++, both malloc
and new
are used for memory allocation, but they serve different purposes and behave differently. While both can allocate memory dynamically, new
is more suitable for C++ programs because it integrates with the object-oriented features of C++ and automatically handles object construction and destruction.
What is malloc?
malloc
(memory allocation) is a standard library function from the C programming language, used to allocate a block of memory of a specified size on the heap. It returns a pointer to the allocated memory. If the memory allocation fails, it returns NULL
.
Syntax of malloc
:
Syntax of malloc
pointer = (dataType*) malloc(size_in_bytes);
malloc
does not initialize the allocated memory, meaning it may contain garbage values. To properly initialize the memory, you would need to use functions like memset
.
What is new?
new
is a C++ keyword that allocates memory on the heap for a single object or an array of objects. Unlike malloc
, new
initializes the memory and calls the constructor (for class objects). Additionally, new
automatically returns a pointer of the correct type, eliminating the need for casting as required by malloc
.
Syntax of new
:
Syntax of new
pointer = new dataType; // Allocates memory for one element
pointer = new dataType[size]; // Allocates memory for an array
Differences between malloc and new
Feature | malloc | new |
---|---|---|
Library | C Standard Library (stdio.h) | C++ Keyword |
Memory Initialization | Does not initialize memory (may contain garbage values) | Initializes memory (calls constructor for objects) |
Return Type | Returns void* (needs casting) |
Returns the correct type pointer (no casting required) |
Constructor Call | Does not call constructors | Calls constructors for class objects |
Deallocation | Requires free() |
Requires delete or delete[] |
Example of malloc
The following example shows how to use malloc
for dynamic memory allocation in C++:
Code Example: malloc
#include <iostream>
#include <cstdlib> // for malloc
using namespace std;
int main() {
// Dynamically allocate memory for an integer using malloc
int* ptr = (int*) malloc(sizeof(int));
if (ptr == NULL) {
cout << "Memory allocation failed" << endl;
return -1;
}
// Assign value to allocated memory
*ptr = 10;
cout << "Value: " << *ptr << endl;
// Free the allocated memory
free(ptr);
return 0;
}
Output
Example of new
Here's an example that demonstrates how to use new
for dynamic memory allocation:
Code Example: new
#include <iostream>
using namespace std;
int main() {
// Dynamically allocate memory for an integer using new
int* ptr = new int;
// Assign value to allocated memory
*ptr = 20;
cout << "Value: " << *ptr << endl;
// Free the allocated memory
delete ptr;
return 0;
}
Output
Pro Tip:
💡 Pro Tip
While malloc
is a low-level memory allocation function from C, new
is preferred in C++ for its better integration with object-oriented features, like constructors and destructors. Moreover, new
is type-safe, meaning it automatically returns the correct pointer type, unlike malloc
, which requires casting. For array allocation with new
, always use delete[]
to ensure proper deallocation.