Overview - new operator
What is it?
The new operator in C++ is used to create objects or variables dynamically on the heap memory during program execution. Unlike regular variables that live on the stack and disappear when a function ends, objects created with new stay alive until you explicitly delete them. This allows programs to use memory flexibly and manage resources that last longer than a single function call. The new operator returns a pointer to the allocated memory where the object lives.
Why it matters
Without the new operator, programs would be limited to fixed-size memory determined at compile time or by stack size, making it hard to handle data that changes size or lives beyond a function. Dynamic memory allocation lets programs grow and shrink data as needed, like storing user input or managing complex data structures. Without it, many modern applications like games, databases, or user interfaces would be inefficient or impossible.
Where it fits
Before learning the new operator, you should understand basic variables, pointers, and memory layout (stack vs heap). After mastering new, you can learn about smart pointers, memory management techniques, and advanced resource handling like RAII (Resource Acquisition Is Initialization).