Sunday, 18 October 2015

Low Level Programming Summary 4, 18th October

Low Level Programming Session Summary

18th October

This session of low level programming we covered a lot more of the advanced capabilities of OOP. 

We discussed arrays and how to dynamically allocate them in memory, which is useful, before I only thought they were able to be created on the stack. To do so, one must declare a pointer in the class, then set up the array in the constructor for that class, thus dynamically allocating that chunk of memory. Array memory is stored in one long line, altogether. 

Multi dimension arrays are declared in the same way as normal arrays, and they are also stored in the same way, one long line of integers. The program doesn't need to know about two dimensions, it just needs to know where to look in memory. Multi dimension arrays are purely for our own benefit, to make sense of what we are creating. We can therefore have two attributes to a list of objects to help us identify them. 

Another note about these is that we shouldn't delete parts of an array, it will lead to memory leak. We should delete the whole array at the same time, otherwise we might get fragmented code. 

Recursion could be a useful replacement for a while loop. This is where a function calls itself inside it's own function, mostly used as some form of iteration. The function essentially loops forever until it finds an exit criteria which can be set by the user and then allow the function to return a value. Each time a function does this however, it creates a new stack frame which will slow down the program. Eventually, so much of the cpu will be taken up, it will have to crash or just stop completely because it can't allocate any more memory. This is best used when you absolutely have to.

Arrays are essentially collections of the same type of data which are stored in a block of memory. They're much easier to handle than single pieces of data stored all over the place. After all, when the memory is assigned, it does so randomly, and one cannot predict where those addresses might be. If stored in an array, we will know that all of our pieces of data will be next to each other.

We also looked at constant data, which is data we will know will take a constant value, useful for setters and main sets of data.

I learnt a lot this session and I'm looking forward to the next one, working more on the dungeon death game.

No comments:

Post a Comment