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 “ >>&num; → 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 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

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<<&num; →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 (*)

#include<bits/stdc++.h>
using namespace std ;
int main()
{
int a = 7 ;
int b = 17 ;
int *c = &b;
*c = 7 ;
cout<<a<<” “<<b<<endl ; //7 7
c = &a ;
*c = 17 ;
cout<<a<<” “<<b<<endl; //17 7
}

Student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store