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.
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 |