Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new operator for memory allocation on heap

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.

like image 458
Chander Shivdasani Avatar asked Feb 09 '11 06:02

Chander Shivdasani


People also ask

Which operator is used for memory allocation?

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.

How do I allocate memory with heap?

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.

Does new allocate on heap?

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.

Does new operator allocate memory in stack?

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.


1 Answers

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:

  1. Allocate memory to hold the requested object by using operator new.
  2. Invoke the object constructor, if any. If this throws an exception, free the above memory with operator delete, then propagate the exception.
  3. Return a pointer to the newly-constructed object.

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.

like image 142
templatetypedef Avatar answered Sep 28 '22 12:09

templatetypedef