- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
C++ Memory Map
Since pointers have a close relationship with memory, as they store the address of a memory location and also facilitate the dynamic memory allocation routines of C++, we must understand the way C++ organizes memory for its programs. After compiling a program, C++ creates four logically distinct regions of memory that are used for four distinct and specific functions.
- One region in the memory holds the compiled code of the program. Every instruction and every function of the program start at a particular address.
- The next region is the memory area, where the global variables of the program are stored. Global variables remain in memory as long as the program continues.
- The third region, known as "Stack," is used for many things while your program executes. The stack is used for holding the return address at function calls, arguments passed to the functions, and local variables for functions. The local variables remain in memory as long as the function continues, and after that, they are erased from the memory. The stack also stores the current state of the CPU.
- The heap memory area is a region of free memory from which chunks of memory are allocated via C++'s dynamic memory allocation functions.
Dynamic and Static Allocation of Memory in C++
The golden rule of computers states that anything and everything (data or instructions) that needs to be processed must be loaded into internal memory before its processing takes place. Therefore, every piece of data and instruction that is being executed must be allocated some space in the main (internal) memory. The main (internal) memory is allocated in these two ways:
Static Memory Allocation in C++
When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as "static memory allocation." For example, when you declare a variable as follows:
short avar;
The computer knows what the length of a short variable is in this case. Generally, it is 2 bytes. So, a chunk of 2 bytes of internal memory will be allocated to the variable "avar" during compilation itself. Thus, it is an example of static memory allocation.
Dynamic Memory Allocation in C++
When the amount of memory to be allocated is not known beforehand, but rather it is required to be allocated as and when required during runtime (when the program is actually executing), the allocation of memory at run time is referred to as "dynamic memory allocation."
C++ offers these two operators for dynamic memory allocations:
- new
- delete
The operator "new" allocates the memory dynamically and returns a pointer storing the memory address of the allocated memory. The operator "delete" deallocates the memory (the reverse of new) pointer by the given pointer.
The article "C++ pointers" already contains a section titled "Dynamic memory allocation in C++" that goes into detail on the subject.
Free Store
For dynamic memory allocation, programs are given access to a pool of unallocated heap memory known as "Free Store."
Every program is provided with a pool of unallocated heap memory that it may utilize during execution. This pool of available memory is referred to as the "free store" of the program.
The allocated free store memory is unnamed. Objects allocated in the free store are manipulated indirectly through pointers. Another aspect of free storage is that the allocated memory is uninitialized. The programmer is responsible for initializing it explicitly.
The free store memory is allocated through the use of the new operator and deallocated through the delete operator.
Free store memory is dynamically allocated during runtime, and static memory allocation takes place during compile time. The object's extend is defined as the length of time an object remains in memory during the execution of a program.Global variables or variables with file scope are referred to as having a static extent. That means the storage is allocated to them before the program's start-up and remains bound to the variable throughout the program's execution.
Variables having local scope are spoken of as having local scope. Storage is allocated to them at each entry into the local scope (i.e., as soon as their local scope starts), and on exit (from the local scope), the storage is freed up. A static extend is a local variable with a static specifier.
Note: The storage allocated through the use of a new operator remains bound to the object until it is explicitly deallocated by the programmer.
Objects with dynamic extend are those that are dynamically allocated on free store.
That means the dynamically allocated objects do not have any predefined scope. They are kept in memory until they are explicitly removed with delete.
« Previous Tutorial Next Tutorial »