Pointers

Pointers and 2-D Arrays

Pointers and Multi-Dimensional Arrays

  • 3-D Arrays and more
  • Need to work on and insert notes

Pointers and Dynamic Memory - Stack vs Heap

  • All about storing memory on a computer and the difference in storing memory on the Heap vs the Stack

  • new keyword and delete keywords

How is memory from a computer programmer stored in the computer Architeture?

#include <iostream>
using std::cout;
using std::endl;

int total;

int Square(int x)
{
    return x*x;
}

int SquareOfSum(int x, int y)
{
    int z = Square(x+y);
    return z;
}

int main()
{
    int a = 4, b = 8;
    total = SquareOfSum(a,b);

    cout << "Output: " << total << endl;
}

What happens in the memory when the code above is executed?

Heap vs Stack in Dynamic Memory

If we have a scenario like we have to decide how large the array will be based on some paramter during runtime then it is a problem with stack.

For all these requirements like allocating large chunk of memory or keeping variable in the memory till the time we want, we have heap

Unlike stack, Heap size is not fixed and its size can vary during the lifetime of the application and there is not rule for allocation or deallocation of the memory. We as programmers can control how much memory to use from the heap till what to keep the data in the memory during the application lifetime.

You can look at Heap as one large free pool of memory available to us that we can use flexibly as per our need.

Heap is also called Dynamic Memory Allocation

Note: Heap is also a Data Structure, I’ve covered in a little in Python, in my Data Structures and Algorithms page!

Example of Using Heap

Any memory which is allocated using the new keyword is cleared off the Heap by calling the delete keyword.

#include <iostream>
using std::cout;
using std::endl;


int main()
{
    int a; // Goes on Stack
    int *p;

    p = new int;
    *p = 10;

    delete p; 
    
    p = new int;
    *p = 20;
}

We can control when to free anything on the Heap

If we wanted tro store an Array on the Heap, then all we need to do is to use the new keyword and pass the size of the Array need.

We get the Base Address of the Array. See example below:

#include <iostream>
using std::cout;
using std::endl;


int main()
{
    p = new int[20];
    delete p;
}

If new is not able to find any free block of memory, it’s not able to allocate any memory on the Heap it returns NULL. So for error handling we need to this and we need to write code correct.

"Insert Images" "Insert Images" "Insert Images" "Insert Images"

Dynamic Memory Allocation in C++

  • Need to Add

Pointers as Function returns in C++

"Insert Images"

Let’s look at this code right here, that uses

#include <iostream>
using std::cout;
using std::endl;

int Add(int a, int b)
{
    // Called Function
    cout << "Address a in Add = " << &a <<  endl;
    int c = a + b;
    return c;

}
int main()
{
    // Calling Function

    int a = 2, b = 4;
    cout << "Address a in main = " << &a <<  endl;

    // Call by Value
    int c = Add(a,b); // Value of a of main is copied to a of Add
                      // Value in b of main is copied to b of Add

    cout << "Sum = " << c << endl; 
    return 0;
}

Output

Address a in main = 0x7ffee1a35608
Address a in Add = 0x7ffee1a355dc
Sum = 6

We can see that the a in Add and the a in main are not the same as we print the addresses of the a’s. Which means these variables are not the same, they’re at different memory addresses.

The names of variables are local or specific to a particular function

Passing the Addresses

Lets pass the addresses of a and b to the Add function

"Insert Images" "Insert Images" "Insert Images"


#include <iostream>
using std::cout;
using std::endl;

int Add(int* a, int* b)
{
    // Called Function
    // a and b are pointers to integers
    cout << "Address a in Add = " << &a <<  endl;
    cout << "Value in a of Add (address of a of main) = " << a <<  endl; // Should give address of a in main
    cout << "Value at address stored in a of Add = " << *a <<  endl;

    int c = (*a) + (*b);
    return c;

}
int main()
{   // Calling Function
    int a = 2, b = 4;
    cout << "Address a in main = " << &a <<  endl;

    // Call by Value
    int c = Add(&a,&b); // a and b are integers local to main

    cout << "Sum = " << c << endl; 
}

Output:

Address a in main = 0x7ffeeb23e60c
Address a in Add = 0x7ffeeb23e5d8
Value in a of Add (address of a of main) = 0x7ffeeb23e60c
Value at address stored in a of Add = 2
Sum = 6