C++ Core
The new Keyword
new = allocate on the heap + run the constructor; delete = destructor + free.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
newis an operator that does two things: allocates heap memory (viaoperator new, usuallymalloc) and calls the constructor. Returns a typed pointer.delete= destructor + free.- Sizes:
new int(4B, garbage),new int()(4B, zero-init),new int[n](n can be a runtime value — dynamic array). - Pairing (mismatch = UB):
new↔delete,new[]↔delete[],malloc↔free. Never cross them. newvsmalloc: new calls ctor/dtor, returns a typed pointer (no cast), and throwsstd::bad_allocon failure; malloc = raw bytes,void*, returnsnullptr, no ctor.- Heap layout: a fixed-size header sits before
p(stores the block size);free/deletedop - headerSize(offset is a compile-time constant) to find it.new[]also stores a count cookie sodelete[]runs every destructor. (Deep dive: the Heap layout page linked at the bottom of this lesson.) - Modern C++: prefer smart pointers (
make_unique/make_shared) over rawnew/delete.
Syntax and matching delete
int* a = new int; // 4 bytes, uninitialized (garbage)
int* b = new int(); // 4 bytes, zero-initialized
int* arr = new int[n]; // runtime-sized array (n from a variable)
Entity* e = new Entity(); // allocate + run constructor
delete a; // matches new
delete b;
delete[] arr; // matches new[] (runs each destructor)
delete e; // destructor + free Deep dive
Go deeper
Watch out
Easily confused
new vs malloc
new calls the ctor + returns a typed pointer + throws bad_alloc on failure. malloc = raw bytes only, void*, returns nullptr, no ctor.
delete vs free
delete = destructor THEN free. free = free only (no destructor).
delete vs delete[]
delete = one object. delete[] reads the stored element count and runs the destructor for every element.
header offset vs block size
Header offset = fixed constant known by the allocator (so
p - headerSize works). Block size = variable, stored in the header and read at free time.Test yourself
8 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.