FAQ Part 9: Memory Management

How is memory allocated in C++?

  • Static allocation: Done at compile-time
  • Dynamic allocation: Done at runtime using new and delete

What is the difference between new and malloc()?

Featurenewmalloc()
LanguageC++C
InitializationYesNo
Type SafetyYesNo
Return TypeExact typevoid*

What are smart pointers?

Smart pointers automatically manage memory:

  • unique_ptr
  • shared_ptr
  • weak_ptr