What Does New Do In C++

4 min read

What Does new Do in C++?

The new operator in C++ is the language's primary tool for dynamic memory allocation. When you write new, you are asking the program to reserve memory at runtime — not at compile time — from a region called the free store (often loosely called the heap). But new does more than hand you a block of bytes: it also constructs an object in that memory and returns a properly typed pointer to it. That combination of allocation plus construction is what separates new from a plain memory allocator like malloc, and it is the reason new is central to how C++ manages objects whose size or lifetime cannot be known in advance Easy to understand, harder to ignore. Nothing fancy..

Understanding new deeply means understanding three things: what it allocates, what it constructs, and what you are responsible for afterward. Get those three right and you avoid the most common memory bugs in C++ Simple, but easy to overlook..

The Two Jobs of new

A new expression performs two distinct operations, in order:

  1. Allocation — it calls an allocation function (by default operator new) to obtain a raw block of memory large enough and aligned correctly for the requested type.
  2. Construction — it runs the type's constructor in that memory, turning raw bytes into a live object.

If construction throws an exception, the memory is automatically released, so you never leak a half-built object. That guarantee is built into the language and is one of the quiet advantages of new over manual allocation.

The expression then returns a pointer of the correct type — T* for new T, or T* to the first element for new T[n].

new vs malloc: Why It Matters

Feature new malloc
Returns Typed pointer (T*) void* (needs a cast)
Calls constructor Yes No
Failure behavior Throws std::bad_alloc Returns nullptr
Size calculation Automatic Manual (sizeof)
Overloadable per class Yes No

And yeah — that's actually more nuanced than it sounds.

With malloc, you get bytes and nothing else. Which means with new, you get a fully initialized object. For any type with a constructor — std::string, std::vector, your own classes — malloc is simply the wrong tool, because it never runs the constructor and never establishes class invariants Not complicated — just consistent..

The Different Forms of new

Scalar new

int* p = new int(42);        // one int, value-initialized to 42
Widget* w = new Widget();    // default-constructed Widget

Note the subtle difference: new int leaves the value uninitialized, while new int() zero-initializes it. For class types, the parentheses determine whether you get value-initialization or default-initialization.

Array new[]

int* arr = new int[10];          // 10 uninitialized ints
Widget* ws = new Widget[5];      // 5 default-constructed Widgets

new[] allocates space for the whole array and default-constructs every element. In practice, for types with non-trivial destructors, the runtime typically stores a hidden array cookie recording the element count, so that delete[] knows how many destructors to run. This is why mixing new[] with plain delete is undefined behavior.

Placement new

#include 
alignas(Widget) unsigned char buffer[sizeof(Widget)];
Widget* w = new (buffer) Widget();   // construct in existing memory

Placement new performs no allocation at all. It simply calls the constructor at the address you supply. Even so, this is essential in embedded systems, custom allocators, memory pools, and containers like std::vector that manage their own storage. Remember that you must call the destructor manually — delete would try to free memory that was never allocated by new That's the part that actually makes a difference..

nothrow new

Widget* w = new (std::nothrow) Widget();
if (!w) { /* handle failure */ }

This form returns nullptr instead of throwing when allocation fails. It exists mainly for legacy code and environments where exceptions are disabled; in modern C++, exceptions are usually the better default.

What Happens Step by Step

When the compiler sees new Widget(1, 2):

  1. It computes sizeof(Widget) and the required alignment.
  2. It calls operator new(size) — which may be the global version or a class-specific overload.
  3. If allocation succeeds, it calls Widget::Widget(1, 2) at that address.
  4. If the constructor throws, it calls the matching operator delete and propagates the exception.
  5. It returns the Widget*.

Since C++17, the standard also guarantees that if the allocation function is replaced or overloaded, the correct matching deallocation function is selected — closing a long-standing loophole.

Freeing Memory: delete and delete[]

Every new must be paired with exactly one delete, and every new[] with exactly one delete[].

delete p;        // for new
delete[] arr;    // for new[]

delete does the reverse of new: it runs the destructor, then releases the memory. Mism

Just Finished

Latest from Us

These Connect Well

On a Similar Note

Thank you for reading about What Does New Do In C++. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home