C++ Interview Questions - Memory

beginner
16 min

C++ Interview Questions - Memory

Welcome to CodeYourCraft's comprehensive guide on C++ Memory Interview Questions! In this lesson, we'll dive deep into understanding memory management in C++, a crucial aspect for any developer. Let's get started!

Table of Contents

  1. Variables and Memory Allocation 1.1. Static and Dynamic Memory Allocation 1.2. Stack and Heap Memory

  2. Memory Leaks and Pointers 2.1. Pointers in C++ 2.2. Pointers and Memory Leaks 2.3. Smart Pointers

  3. Memory Management Techniques 3.1. New, Delete, and Delete[] 3.2. Placement New

  4. Quiz


1. Variables and Memory Allocation

1.1. Static and Dynamic Memory Allocation

In C++, we have two types of memory allocation: static and dynamic.

  • Static Memory is allocated at compile time. The size is fixed and cannot be changed at runtime. Each object occupies a fixed memory location for the lifetime of the program.
cpp
int arr[10]; // static array
  • Dynamic Memory is allocated at runtime. The size can be changed depending on the needs of the program.
cpp
int* p = new int[10]; // dynamic array

šŸ’” Pro Tip: Use static memory when the size of the data is known at compile time, and dynamic memory when the size is not known or may change during execution.


1.2. Stack and Heap Memory

In C++, memory is divided into two main sections: stack and heap.

  • The stack is a region of memory used for function call and return addresses, local variables, and recursive calls. Memory in the stack is managed automatically by the operating system and C++ compilers.
cpp
void func() { int a = 10; // local variable on the stack }
  • The heap is a region of memory where dynamic memory allocation takes place. Memory in the heap is managed by the new and delete operators or memory allocation libraries such as malloc(), calloc(), realloc(), and free().
cpp
int* p = new int[10]; // dynamic array on the heap

šŸ“ Note: Stack memory is faster but has a limited size, while heap memory is slower but has a larger size.


2. Memory Leaks and Pointers

2.1. Pointers in C++

A pointer is a variable that stores the memory address of another variable. In C++, you can declare a pointer using the asterisk (*) symbol.

cpp
int x = 10; int* p = &x; // pointer to x

2.2. Pointers and Memory Leaks

If a dynamically allocated memory is not freed properly, it results in a memory leak. This can lead to the consumption of unnecessary memory and can negatively impact the performance of your program.

cpp
int* p = new int[10]; // ... use the memory ... // forget to free the memory

šŸ’” Pro Tip: Always free dynamically allocated memory using the delete or delete[] operator to avoid memory leaks.


2.3. Smart Pointers

To overcome the challenges of managing raw pointers and memory leaks, C++ offers smart pointers. Smart pointers are a class template that encapsulates raw pointers and provides automatic memory management.

cpp
#include <memory> std::unique_ptr<int> p = std::make_unique<int>(10); // smart pointer automatically frees the memory when it goes out of scope

3. Memory Management Techniques

3.1. New, Delete, and Delete[]

The new operator allocates memory and returns a pointer to the allocated memory. The delete operator deallocates memory, and the delete[] operator is used for arrays.

cpp
int* p = new int[10]; // ... use the memory ... delete[] p; // deallocate the memory

3.2. Placement New

The placement new operator allows you to control the memory location where a new object is created.

cpp
int* p = new int[10]; int* q = p + 5; // fifth element in the array placement new (uninitialized_default_construct<int>) q; // create a new object at q

4. Quiz

Quick Quiz
Question 1 of 1

Which operator is used to deallocate memory for arrays in C++?


And that's a wrap for our comprehensive guide on C++ Memory Interview Questions! Keep practicing and improving your memory management skills to become a proficient C++ developer. Happy coding! šŸŽÆ