Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

new-delete

new and delete

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches new and delete: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship new and delete in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply new and delete in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

new and delete allocate and release heap memory for single objects and arrays. They call constructors/destructors—unlike malloc/free in C.

Single object

int* p = new int(5);
delete p;
p = nullptr;

In modern C++, prefer std::make_unique and containers so cleanup is automatic.

Important interview questions and answers

  1. Q: new vs malloc?
    A: new invokes constructors and pairs with delete; malloc is C-style raw bytes.
  2. Q: double delete?
    A: Undefined behavior—set pointer to nullptr after delete or use smart pointers.

Self-check

  1. What calls the destructor for heap objects?
  2. Modern alternative to raw new/delete?

Pitfall: Every new needs matching delete (or array forms)—prefer std::make_unique so destructors run automatically.

Interview prep

new vs malloc?

new invokes constructors and pairs with delete; malloc allocates raw bytes in C style.

Double delete?

Undefined behavior—use smart pointers or set pointer to nullptr after delete.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • new[] vs new?
  • Leak detect how?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump