Pointers and references – part 1

An pointer and a reference, are ways of accessing data from memory locations which is where the data is stored.   A pointer is a place in memory where a memory location is stored for where the actual data is, e.g. if the data is stored in memory location 0×004 and the data value is a integer value of 15 the memory would look like this

Memory location Value
0×002 NULL
0×004 15 – the integer value
0×006 NULL

The pointer has a memory location of lets say, 0×010 and to start with it is set to NULL

int *pointerInt = NULL;
Memory location Value
0×010 NULL

If you then set the pointer to point the a integer value of 15 as

int intergerValue = 15;
 
// the pointer setting
pointerInt = &intergerValue;

The & means the memory location (address/reference point).

So the memory layout from earlier would be

Memory location Value
0×010 0×004

The pointer value is set to the memory location of the actual data, thus to access the data you will need to dereference the pointer by.

cout << *pointerInt << endl;

The * means to return where the pointer is pointing to (that is the dereferencing part), so what it will return is the value in memory location 0×004 which is 15.

Here is another example that you can copy and paste into a c++ editor and compile it up :) .

It is a class example, of how the difference is accessing the pointer new class (-> you are pointing to the data) and how a normal variable accessing the data ( . it is part of the memory location already :) )

#include <iostream>
#include <stdio.h>
 
using namespace std;
 
class classToPointTo
{
  private :
    int value;
 
  public :
    classToPointTo() { value = 0;}
 
    classToPointTo(int v) { value = v; }
 
    int returnValue() { return value; }
 
    void setValue(int g) { value = g; }
};
 
int main()
{
    // if you want to create a pointer to a class, that is not already setup e.g. delcared then in memory
    classToPointTo* pointerToClass = new classToPointTo();
    // to call functions you just put "->" after the variable name
    cout << "The pointer value : " << pointerToClass->returnValue() << endl;
    // to set the value and then display the new value
    pointerToClass->setValue(44);
    cout << "The new pointer value : " << pointerToClass->returnValue() << endl;
 
    // a normal way to declare a class is 
    classToPointTo normalC;
    // and to reference the methods you use the "."
    cout << "The normal value : " << normalC.returnValue() << endl;
    // to set the value and display
    normalC.setValue(22);
    cout << "The new normal value : " << normalC.returnValue() << endl;
 
    cout << "The memory reference for the pointer = " << pointerToClass << endl;
    cout << "The memory reference for the normalC = " << &normalC << endl;
 
    // store the memory reference point for the pointerToClass pointer
    unsigned long pointerInt = (unsigned long)pointerToClass;
    printf("pointerInt : 0x%x that is value of a pointerInt value, this is the place in memory where the pointerToClass is stored\n", (unsigned int)pointerInt);
    // and here is where the fun starts, since a pointer can point to any place in memory
    // then you can reference (&) the normalC class and use that as a memory place instead
    pointerToClass = &normalC;
 
    cout << "The shall be the same since re-pointed pointerToClass to normalC" << endl;
 
    cout << "The memory reference for the pointer = " << pointerToClass << endl;
    cout << "The memory reference for the normalC = " << &normalC << endl;
 
    // the pointerToClass is now pointing to the normalC class and any adments to it will reflect in both
    // normalC and pointerToClass.  But with doing this, you loss the reference point that setup before
    cout << "The new pointer value : " << pointerToClass->returnValue() << endl;
 
    // set the pointerToClass value equal to 46
    pointerToClass->setValue(46);
    // they are both changed
    cout << "Changed pointerToClass value to 46, they are the same now, since pointing to the same place" << endl;
    cout << "The pointer value : " << pointerToClass->returnValue() << endl;
    cout << "The normal value : " << normalC.returnValue() << endl;
 
    // now change it back to the orginal place, have to do case back to a classToPointTo* (pointer)
    pointerToClass = (classToPointTo*)pointerInt;
    // it is back the before value
    cout << "The pointer value : " << pointerToClass->returnValue() << endl;
    return 0;
}

And here is the output, if you notice that the memory locations

The pointer value : 0
The new pointer value : 44
The normal value : 0
The new normal value : 22
The memory reference for the pointer = 0x13ba010
The memory reference for the normalC = 0x7fff4980d390
pointerInt : 0x13ba010 that is value of a pointerInt value, this is the place in memory where the pointerToClass is stored
The shall be the same since re-pointed pointerToClass to normalC
The memory reference for the pointer = 0x7fff4980d390
The memory reference for the normalC = 0x7fff4980d390
The new pointer value : 22
Changed pointerToClass value to 46, they are the same now, since pointing to the same place
The pointer value : 46
The normal value : 46
The pointer value : 44

Tags: ,

Leave a Reply

Spam protection by WP Captcha-Free