Memory in C++
By: Mubasshir Ahmed
Tags: Cpp, Memory Management

To understand this I recommend doing this course: Object-Oriented Data Structures in C++ by Wade Fagen Ulmschneider .

Memory in C++ is divided into two parts.

1) Stack Memory

2) Heap Memory

Stack Memory: All variables declared inside the function are stored in stack memory.

Heap Memory: This is unused memory of the program and can be used to allocate the memory when the program runs.

By default, every variable in C++ placed in stack memory.

Every variable in C++ has four things.

1) A name

2) A type

3) A value

4) A location in memory

int primeNumber = 7 ;

int num = 7 ;

cout<<”value “<< num ; → output : 7

cout<<”memory “ >># → output : 0x61ff0c

& → by using this operator in C++ we return the memory address of a variable.

&num → 0x61ff0c → memory address is hexa-decimal value .

Stack Memory life-Cycle :

Stack memory is associated with the current function and the memory lifecycle is tied to the function -> when the function returns or ends, the stack memory of that function is released.

Stack memory always starts from high addresses and goes down.

void foo() {

int x =37;

cout<<&x<<endl; → output : 0x61fedc . here edc = 3804

} int main() {

int num = 7 ;

cout<<&num<<endl; → output : 0x61ff0c . here f0c = 3852

}

The memory address of “num” (0x61ff0c) is greater than the memory address of “x” (0x61fedc ) because Stack memory always starts from high addresses and goes down. the 1st stored variable’s address is always then the next one.

Pointer

A pointer is a variable that stores the memory address of data. In C++, a pointer is defined by adding an * to the type of the variable.

Deference

Deference is used to access or manipulate data contained in a memory location pointed to by a pointer.

int num = 7 ;

cout<<num; → output : 7

cout<<# →output : 0x61ff0c int * p = &num ;

cout<<p; → output : 0x61ff0c cout<<&p ; → output : 0x61ff08 [ this value is the address of &num . here we store the address cout<<*p ; → output : 7 of num as a data ] &p = 42 ;

cout<<num; → 42

Heap Memory

If memory needs to exist for longer than the lifecycle of the function, we must use heap memory.

The only way to create heap memory in C++ is with the “new” operator.

* The new operator in C++ will always do three things.

1) Allocate memory on the heap for the data structure

2) Initialize the data structure

3) Return a pointer to the start of the data structure.

The memory is only reclaimed by the system when the pointer is passed to the “delete” operator.

Stack memory point to the heap memory. here “ int * “ , “ numPtr “ store on the

Stack and “ int “ store to the heap.

Heap memory starts from low addresses and goes up.

int * numPtr = new int ;

cout<<*numPtr; → output : “ mysterious value “

cout<< numPtr; → output : “memory address”

cout<<&numPtr → output: “address” *numPtr =42 ;

cout<<*numPtr; → output: “42”

cout<< numPtr; → output : “memory address of 42”

cout<<&numPtr → output: “address of 42” delete numPtr → numPtr = nullptr

cout<<*numPtr → mysterious value

Null Pointer

nullptr is a pointer that points to the memory address 0x0 .

→ nullptr means ‘no-where”;

→ 0x0 address reserved and never used any system

→ 0x0 will always generate an ‘’segmentation fault” when accessed.

Arrow

When an object is stored via pointer access can be made to the member function using the “ → ” operator.

Practice Code :

#include<bits/stdc++.h>

using namespace std ;

int main()

{

int i = 2 , j = 4 , k = 8;

Int *p = &i;

int *q = &j;

int *r = &k; k = i ;

cout<<i<<” “<<j<<” “<<k<<” “<<*p<<” “<<*q<<” “<<*r<<endl; //2 4 2 2 4 2 p = q ;

cout<<i<<” “<<j<<” “<<k<<” “<<*p<<” “<<*q<<” “<<*r<<endl; //2 4 2 4 4 2 *q = *r ;

cout<<i<<” “<<j<<” “<<k<<” “<<*p<<” “<<*q<<” “<<*r<<endl; //2 2 2 2 2 2

return 0 ;

}

2)

#include<bits/stdc++.h>

using namespace std ;

int main()

{

int *x = new int ;

int &y = *x ;

y = 4 ;

cout<<&x<<endl; // 0xffff4567

cout<<x<<endl; // 0x12345

cout<<*x<<endl; // 4 cout<<&y<<endl; // 0x12345

cout<<y<<endl; // 4

// cout<<*y<<endl; -> this line occur error

return 0 ;

}

3)

#include<bits/stdc++.h>

using namespace std ;

int main()

{

int *p ;

int *q ;

p = new int ;

q = p ;

*q = 8 ;

cout<<*p<<endl; // 8

q = new int ;

*q = 9;

cout<<*p<<endl; // 8

cout<<*q<<endl; // 9

}

4) Array

#include<bits/stdc++.h>

using namespace std ;

int main()

{

int *x ;

int size =3 ;

x = new int[size];

for(int i=0;i<size;i++)

{

x[i] = i+3;

}

}

5) Swap using dereference (*)