SlideShare a Scribd company logo
3
Most read
4
Most read
7
Most read
C++ Memory
Management
Content
1. C++ Memory Segments
2. Program in RAM
3. Dynamic Memory
4. Call Stack
5. Call Stack Segment
6. Stack in Action
C++ Memory Segments
 A program is typically divide into different memory
areas called segments.
 Code Segment
 It also called text segment where the compiled program sits in
memory. The code segment is typically read-only.
 BSS Segment
 It is also called the uninitialized data segment, where zero-
initialized global and static variables are stored.
 Data Segment
 It is also called initialized data segment where initialized global
and static variables are stored
 Heap
 It holds dynamically allocated variables.
 Stack
 It stores function parameters, local variables, and other function-
related information.
Program in RAM
Sections:
1. Read only
a) .text: Program code.
b) .rodata: constants (const modifier)
and strings.
2. Global and Static
a) .data: Initialized global and static
variables (startup value ≠ 0)
b) .bss: Uninitialized global and static
variables (zero value on startup)
Dynamic Memory
Heap Segment (also known as the “free store”) keeps
track of memory used for dynamic memory allocation.
In C++, when you use the new operator to allocate memory,
this memory is allocated in the application’s heap segment.
 int *ptr = new int; // ptr is assigned 4 bytes in the heap
 int *array = new int[10]; // array is assigned 40 bytes in the heap
Heap has advantages and disadvantages:
 Allocating memory on the heap is comparatively slow.
 Allocated memory stays allocated until it is specifically
deallocated (beware memory leaks) or the application ends
(at which point the OS should clean it up).
 Dynamically allocated memory must be accessed through a
pointer. Dereferencing a pointer is slower than accessing a
variable directly.
 Because the heap is a big pool of memory, large arrays,
structures, or classes can be allocated here.
Call Stack
Call stack usually referred to as “stack” .
The call stack keeps track of all the active functions from the start of the program to the current point of
execution, and handles allocation of all function parameters and local variables.
The call stack is implemented as a stack data structure.
A stack is a last-in, first-out (LIFO) structure. The last item pushed onto the stack will be the first item
popped off.
Stack has advantages and disadvantages:
1. Allocating memory on the stack is comparatively fast.
2. Memory allocated on the stack stays in scope as long as
it is on the stack. It is destroyed when it is popped off
the stack.
3. All memory allocated on the stack is known at compile
time. Consequently, this memory can be accessed
directly through a variable.
4. Because the stack is relatively small, it is generally not
a good idea to do anything that eats up lots of stack
space. This includes passing by value or creating local
variables of large arrays or other memory-intensive
structures, it leads to stack Overflow
Call Stack Segment
The Call Stack Segment holds the memory used for the call stack.
When a application function call is encountered, the function is pushed onto the call stack. When the current
function ends, that function is popped off the call stack. There is no need erase the memory just leave it to
be overwritten by the next item pushed to that piece of memory.
The stack itself is a fixed-size chunk of memory addresses,
and the “items” we’re pushing and popping on the stack are
called stack frames. A stack frame keeps track of all of the
data associated with one function call.
The “marker” is a register (a small piece of memory in the
CPU) known as the stack pointer (sometimes abbreviated
“SP”). The stack pointer keeps track of where the top of the
call stack currently is.
Call Stack in Action
How the call stack works?
Below is the sequence of steps that takes place when a function is called:
1. The program encounters a function call.
2. A stack frame is constructed and pushed on the stack. The stack frame consists of:
a) The address of the instruction beyond the function call (called the return address). This is how
the CPU remembers where to return to after the called function exits.
b) All function arguments.
c) Memory for any local variables.
d) Saved copies of any registers modified by the function that need to be restored when the function
returns
3. The CPU jumps to the function’s start point.
4. The instructions inside of the function begin executing.
When the function terminates, the following steps happen:
1. Registers are restored from the call stack
2. The stack frame is popped off the stack. This frees the memory for all local variables and arguments.
3. The return value is handled.
4. The CPU resumes execution at the return address
Questions?

More Related Content

PPTX
Type of websites
PPTX
Historical social & economic context of computing
PPT
Semiconductor devices
PDF
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
PPTX
arduino based radar system
PPTX
07. Virtual Functions
PPTX
Inheritance in c++
PPT
machine-learning-with-python (1).ppt
Type of websites
Historical social & economic context of computing
Semiconductor devices
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
arduino based radar system
07. Virtual Functions
Inheritance in c++
machine-learning-with-python (1).ppt

What's hot (20)

PPT
C++ Memory Management
PDF
file handling c++
PPTX
File Management in C
PPTX
Operating system memory management
PDF
Memory management
PPTX
MACRO PROCESSOR
PDF
C++ OOPS Concept
PPTX
C++ decision making
PPTX
virtual function
PPTX
Object oriented programming
PDF
Polymorphism in oop
PDF
loaders and linkers
PPTX
Java Data Types and Variables
PPTX
java interface and packages
PPTX
File in C language
PPTX
Operators in java
PPTX
C functions
C++ Memory Management
file handling c++
File Management in C
Operating system memory management
Memory management
MACRO PROCESSOR
C++ OOPS Concept
C++ decision making
virtual function
Object oriented programming
Polymorphism in oop
loaders and linkers
Java Data Types and Variables
java interface and packages
File in C language
Operators in java
C functions
Ad

Similar to C++ Memory Management (20)

PPTX
STACK 20 INTERVIEW QUESTIONS and answers.pptx
PPTX
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
PDF
Buffer overflow attack
PPTX
C MEMORY MODEL​.pptx
 
PPTX
C MEMORY MODEL​.pptx
 
PPT
Linux memorymanagement
PPT
Malware Analysis - x86 Disassembly
PDF
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
PDF
220 runtime environments
PDF
Buffer overflow attacks
PDF
Embedded C - Lecture 3
PDF
(8) cpp stack automatic_memory_and_static_memory
PPTX
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
PDF
nasm_final
PDF
Run time storage
PDF
Intermediate code optimization Unit-4.pdf
PPTX
Functions with heap and stack
PPTX
Run time administration
STACK 20 INTERVIEW QUESTIONS and answers.pptx
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
Buffer overflow attack
C MEMORY MODEL​.pptx
 
C MEMORY MODEL​.pptx
 
Linux memorymanagement
Malware Analysis - x86 Disassembly
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
220 runtime environments
Buffer overflow attacks
Embedded C - Lecture 3
(8) cpp stack automatic_memory_and_static_memory
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
nasm_final
Run time storage
Intermediate code optimization Unit-4.pdf
Functions with heap and stack
Run time administration
Ad

More from Rahul Jamwal (6)

PPTX
Process & Mutlithreading
PPTX
Searching & Sorting Algorithms
PPTX
Data Structures
PPTX
Virtual Memory Management
PPTX
C++ shared libraries and loading
PPTX
C++ compilation process
Process & Mutlithreading
Searching & Sorting Algorithms
Data Structures
Virtual Memory Management
C++ shared libraries and loading
C++ compilation process

Recently uploaded (20)

PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administraation Chapter 3
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
ManageIQ - Sprint 268 Review - Slide Deck
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Operating system designcfffgfgggggggvggggggggg
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms I-SECS-1021-03
Digital Strategies for Manufacturing Companies
ISO 45001 Occupational Health and Safety Management System
Which alternative to Crystal Reports is best for small or large businesses.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Odoo POS Development Services by CandidRoot Solutions
Adobe Illustrator 28.6 Crack My Vision of Vector Design

C++ Memory Management

  • 2. Content 1. C++ Memory Segments 2. Program in RAM 3. Dynamic Memory 4. Call Stack 5. Call Stack Segment 6. Stack in Action
  • 3. C++ Memory Segments  A program is typically divide into different memory areas called segments.  Code Segment  It also called text segment where the compiled program sits in memory. The code segment is typically read-only.  BSS Segment  It is also called the uninitialized data segment, where zero- initialized global and static variables are stored.  Data Segment  It is also called initialized data segment where initialized global and static variables are stored  Heap  It holds dynamically allocated variables.  Stack  It stores function parameters, local variables, and other function- related information.
  • 4. Program in RAM Sections: 1. Read only a) .text: Program code. b) .rodata: constants (const modifier) and strings. 2. Global and Static a) .data: Initialized global and static variables (startup value ≠ 0) b) .bss: Uninitialized global and static variables (zero value on startup)
  • 5. Dynamic Memory Heap Segment (also known as the “free store”) keeps track of memory used for dynamic memory allocation. In C++, when you use the new operator to allocate memory, this memory is allocated in the application’s heap segment.  int *ptr = new int; // ptr is assigned 4 bytes in the heap  int *array = new int[10]; // array is assigned 40 bytes in the heap Heap has advantages and disadvantages:  Allocating memory on the heap is comparatively slow.  Allocated memory stays allocated until it is specifically deallocated (beware memory leaks) or the application ends (at which point the OS should clean it up).  Dynamically allocated memory must be accessed through a pointer. Dereferencing a pointer is slower than accessing a variable directly.  Because the heap is a big pool of memory, large arrays, structures, or classes can be allocated here.
  • 6. Call Stack Call stack usually referred to as “stack” . The call stack keeps track of all the active functions from the start of the program to the current point of execution, and handles allocation of all function parameters and local variables. The call stack is implemented as a stack data structure. A stack is a last-in, first-out (LIFO) structure. The last item pushed onto the stack will be the first item popped off. Stack has advantages and disadvantages: 1. Allocating memory on the stack is comparatively fast. 2. Memory allocated on the stack stays in scope as long as it is on the stack. It is destroyed when it is popped off the stack. 3. All memory allocated on the stack is known at compile time. Consequently, this memory can be accessed directly through a variable. 4. Because the stack is relatively small, it is generally not a good idea to do anything that eats up lots of stack space. This includes passing by value or creating local variables of large arrays or other memory-intensive structures, it leads to stack Overflow
  • 7. Call Stack Segment The Call Stack Segment holds the memory used for the call stack. When a application function call is encountered, the function is pushed onto the call stack. When the current function ends, that function is popped off the call stack. There is no need erase the memory just leave it to be overwritten by the next item pushed to that piece of memory. The stack itself is a fixed-size chunk of memory addresses, and the “items” we’re pushing and popping on the stack are called stack frames. A stack frame keeps track of all of the data associated with one function call. The “marker” is a register (a small piece of memory in the CPU) known as the stack pointer (sometimes abbreviated “SP”). The stack pointer keeps track of where the top of the call stack currently is.
  • 8. Call Stack in Action How the call stack works? Below is the sequence of steps that takes place when a function is called: 1. The program encounters a function call. 2. A stack frame is constructed and pushed on the stack. The stack frame consists of: a) The address of the instruction beyond the function call (called the return address). This is how the CPU remembers where to return to after the called function exits. b) All function arguments. c) Memory for any local variables. d) Saved copies of any registers modified by the function that need to be restored when the function returns 3. The CPU jumps to the function’s start point. 4. The instructions inside of the function begin executing. When the function terminates, the following steps happen: 1. Registers are restored from the call stack 2. The stack frame is popped off the stack. This frees the memory for all local variables and arguments. 3. The return value is handled. 4. The CPU resumes execution at the return address