pointer arithmetic and dynamic memory allocation
diagram of C++ Memory Management Tutorial
Introduction
In C++, pointer arithmetic and dynamic memory allocation are essential concepts that allow developers to manage memory directly. These features help build efficient applications by controlling how memory is allocated, accessed, and freed at runtime. Understanding these topics is crucial for mastering low-level programming in C++.
Pointer arithmetic means performing mathematical operations on pointers to move through memory locations. This is especially useful when working with arrays and dynamically allocated memory.
++
and --
(increment and decrement)
+
and -
(addition and subtraction)
==
, !=
, <
, >
(comparison between pointers of same type)
When a pointer is incremented, it moves to the next memory location based on the data type size.
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
cout << "First element: " << *ptr << endl;
ptr++; // moves to next integer (4 bytes ahead)
cout << "Second element: " << *ptr << endl;
ptr++;
cout << "Third element: " << *ptr << endl;
return 0;
}
Output:
First element: 10
Second element: 20
Third element: 30
Explanation: Each increment moves the pointer to the next array element because int
size is 4 bytes.
Dynamic memory allocation allows developers to allocate memory at runtime using pointers. This is useful when the size of data is unknown during compile time.
new
— Allocates memory dynamically
delete
— Frees dynamically allocated memory
Let's create a Developer class and allocate it dynamically.
#include <iostream>
using namespace std;
class Developer {
public:
string name;
string language;
Developer(string n, string l) : name(n), language(l) {}
void display() {
cout << "Developer: " << name
<< ", Language: " << language << endl;
}
};
int main() {
// Dynamic allocation
Developer *devPtr = new Developer("Alice", "C++");
devPtr->display();
// Free the memory
delete devPtr;
return 0;
}
Output:
Developer: Alice, Language: C++
Explanation: We created a Developer
object dynamically on the heap using new
, accessed it via pointer, and freed the memory using delete
.
We can allocate an array dynamically and use pointer arithmetic to access elements.
int *scores = new int[3];
scores[0] = 80;
scores[1] = 90;
scores[2] = 100;
int *ptr = scores;
for (int i = 0; i < 3; i++) {
cout << "Score " << i+1 << ": " << *(ptr + i) << endl;
}
delete[] scores;
Output:
Score 1: 80
Score 2: 90
Score 3: 100
Pointer arithmetic depends on the size of the data type.
Always delete
memory allocated using new
to prevent memory leaks.
Use delete[]
for arrays allocated with new[]
.
Be careful with dangling pointers (pointers to freed memory).
Pointer arithmetic and dynamic memory are core features in C++ that give developers full control over how memory is managed at runtime. These concepts help write efficient, high-performance applications and are widely used in systems programming, data structures, and game development.
Mastering these concepts will make you a more confident and skilled C++ developer.