Second week and Pointers!

This week we have gone through some new things that I have never actually done before. The main thing being pointers and memory allocation. Arrays and functions were also brought up, but I already knew the basics around these. Another thing that we learnt was type casting. I have always used the C way of casting, for example (float)variable, but the right way to do it in C++ is to type static_cast(variable); to cast the variable to a float value.

Memory allocation is something very important when programming. There are two memory locations you can access, stack and heap. Stack is memory that handles variables automatically when they are declared, where as heap is something we programmers must allocate memory for new variables we want to use. The benefit of using the heap is that it is so much bigger than the stack. To allocate memory we use the new and delete operators. For example:

 double* doubleVariable = new double;

This creates a pointer that points to a new double in the heap memory. But when allocating memory we must also delete it when we no longer need it. If we don’t do this it will create a memory leak and the space it is taking can’t be used for something else, it is just sitting there being useless. To delete we do as following:

delete doubleVariable;
doubleVariable = nullptr;

The second row sets the pointer to nullptr, making it invalid. We do this to make sure it does not point to some garbage that could mess up your code.

During this week we have learnt a couple of new operators and some of them are a bit hard to distinguish from each other, especially the ones often used with pointers. I will be listing some of the syntax below.

  • & – used to create references.
    int intVariable = 10;
    int& referenceIntVar = intVariable;
    referenceIntVar = 5; // intVariable is also set to 5

    When used in declaration after the data type it will reference to the same memory address as the given variable. You could say that a value has two variables.
    When used in front of a variable you are returning the memory address of that variable.

    int variable = 1;
    std::cout << "Address: " << &variable << std::endl; 

    This will NOT show the value 1, but the memory address to the variable.

  • * – used to create pointers and dereferencing
    When creating a pointer you put the pointer after the data type when declaring it.

    int value = 10;
    int* intPointer = &value;
    std::cout << "addr: " << intPointer << std::endl; // Displays the memory address
    std::cout << "value: " << *intPointer << std::endl; // Displays the value of the variable the pointer is pointing at

    When used in front of the pointers identifier (row 4) you dereference the pointer and is will show the value the pointer is pointing at.

The last two lectures we had this week we created the game Pong. Our lecturer had a live workshop and went through the code of the game. For such a small and simple game there were quite a bit of logic that we had to work out, but I feel that I understood the most part of it.

Best regards,

Sven

Music of the week: Low Hangin’ Fruit – Tenacious D

About Sven Almberg

2014  Programming