I was looking at the signature of new operator. Which is:
void* operator new (std::size_t size) throw (std::bad_alloc);
But when we use this operator, we never use a cast. i.e
int *arr = new int;
So, how does C++ convert a pointer of type void*
to int*
in this case. Because, even malloc
returns a void*
and we need to explicitly use a cast.
You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.
The heap is a large area of memory available for use by the program. The program can request areas, or “blocks”, of memory for its use within the heap. In order to allocate a block of some size, the program makes an explicit request by calling the heap allocation operation.
In classical architectures, the stack and heap grow toward each other to maximize the available space. C++ uses the new operator to allocate memory on the heap.
In the case of memory allocation using placement new operator , since it is created in stack the compiler knows when to delete it and it will handle deallocation of the memory automatically.
There is a very subtle difference in C++ between operator new
and the new
operator. (Read that over again... the ordering is important!)
The function operator new
is the C++ analog of C's malloc
function. It's a raw memory allocator whose responsibility is solely to produce a block of memory on which to construct objects. It doesn't invoke any constructors, because that's not its job. Usually, you will not see operator new
used directly in C++ code; it looks a bit weird. For example:
void* memory = operator new(137); // Allocate at least 137 bytes
The new
operator is a keyword that is responsible for allocating memory for an object and invoking its constructor. This is what's encountered most commonly in C++ code. When you write
int* myInt = new int;
You are using the new operator to allocate a new integer. Internally, the new
operator works roughly like this:
operator new
.operator delete
, then propagate the exception.Because the new
operator and operator new
are separate, it's possible to use the new
keyword to construct objects without actually allocating any memory. For example, the famous placement new allows you to build an object at an arbitrary memory address in user-provided memory. For example:
T* memory = (T*) malloc(sizeof(T)); // Allocate a raw buffer new (memory) T(); // Construct a new T in the buffer pointed at by 'memory.'
Overloading the new
operator by defining a custom operator new
function lets you use new
in this way; you specify how the allocation occurs, and the C++ compiler will wire it into the new
operator.
In case you're curious, the delete
keyword works in a same way. There's a deallocation function called operator delete
responsible for disposing of memory, and also a delete
operator responsible for invoking object destructors and freeing memory. However, operator new
and operator delete
can be used outside of these contexts in place of C's malloc
and free
, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With