Thursday, 8 October 2015

Low-Level Programming_Post3

Low-Level Programming: Post 3 08/10/2015


8th October 2015 is the date of the third lesson on Low-Level Programming.

Unlike the previous lesson that I had, in which the beginning was a recap of what we were taught in the first lesson, the lesson today was a cover of what we were going to be taught today, before the practical side of the lesson, which is in the last hour of the lesson. But before I get to what we were taught today, there were two different features that were shown to us. Both of these were to help us to develop the 'Dev Blogs' and a way to approach the Dungeon Game code provided to us last week.

The first of the two features that was mentioned in the above paragraph was that we were provided with some examples on how to develop the Dev blogs. Some of the examples were good, others were bad.

Despite there being only 1 image on the good side of how to develop the Dev blog, it is/was helpful nevertheless. The image provided what was taught in the lesson, the key points/concepts from the lesson and a personal point of view on how the person who wrote the post felt about what was taught in the lesson. The last point about the person saying their view about the lesson surprised me a little, but I completely understand why he/she provided their personal opinion on the lesson.

There were, however, 3 images that showed how the Dev blogs shouldn't be done. The three examples ranged from saying the computers were 'crap' and not understanding as to why we had to fix some compiler errors, only mentioning some of the main points taught in the lesson and, my personal 'favourite', a post that only had the words "Write blog entry here". These posts also helped me despite being completely rubbish, as it gave us examples on how NOT to do Dev blog posts.

The second of the two features that was mentioned in the paragraph was that we were given a method on how to approach the coding task from last week, in that, some images were provided on listing the various variables, states and so on that are listed on each different object. For example, the first image showed that the 'bat' object contains the following: Attacks, Defends, Is It Alive?, Renders, States and two mini variables connected to the States, which are Has Health and Has Damage Modifier. The player, though sharing some of the characteristics of the bat (enemy), it also has some differences. The 'player' object contains the following: Defends, Attacks, Has Score, Has Health, States and three mini variables connected to the States, which are Has Score, Has Health and Has Damage Modifier. It also showed some OOP graphs on the different variables mentioned in the bat and player object(s).

For example, the bat's graph is as follows...


The player's graph is as follows...

The GameObject's graph is as follows...

After the two parts of the lesson (Dev blogs and 'How to approach the code from last week') and after the break, we were taught some new features and a small recap, since I knew some of the features from last year. Some of them include Memory Allocation, both static and dynamic, stacks, scopes and nullPtr.

Static Memory Allocation
  • Default Behaviour: When we create instances of object memory is automatically allocated on the stack.
  • Lifetime(1): The memory remains allocated until the object or variable falls out of scope.
  • Cleanup: The memory cleanup process is automatic.
  • Lifetime(2): All objects, variables and arrays have a known size throughout the programs run.
  • Management: Removed from the programmer's concern.
Scopes
  • Scopes: Areas in our code where we combine functionality.
  • Organisation: Everything in our program can be seen as organized into these scopes i.e. functions, classes etc.
  • Memory Allocation: When the scope terminates, all the memory that's been statically allocated get's released.
  • Variables: Anything destroyed in this scope won't be accessible. This is why we shouldn't return references or pointers to objects whose lifespan ends when the function does.
Stack
  • Stack: A fast and organized dedicated area in memory. where we store instances of variables and objects at runtime.
  • FILO: First in, last out principal. We push and pop items off the stack.
  • Memory: Memory is managed for you.
  • Static: Everything needs to be a known quantity at program run.
  • Size Limits: It's a limited amount of memory and it's possible to utilise it all. Stack Overflow is 'Game Over'.
  • Local Storage: When the function call ends, all data is popped off of the stack.
Dynamic Memory Allocation
  • Dynamic Memory: The opposite if Static memory. The complier is unable to determine the amount of memory it needs to allocate.
  • Lifespan: Dynamically allocated memory lives independently of the function it's allocated in.
  • Allocation: The programmer is in charge of both allocation and deallocation.
  • Heap: Instead of the stack, dynamic allocation occurs on the heap.
Heap
  • Heap: A region of memory that's not automagically handled for you.
  • Organisation: Unlike the stack, it has no defined structure, you can add and delete memory as you see fit.
  • Allocation: In order to allocate memory dynamically, we use the new keyword.
  • Access: In order to access the memory, we now need to deference a pointer,
Automatic/Dynamic
  • Memory Usage: Dynamic allocation allows us to reserve only the amount of memory we require i.e. less wasted unused portions.
  • Speed: The stack is faster to access than the heap due to the level of indirection a pointer deference incurs.
  • Memory Management: Stack usage means the memory is freed when it's not needed, unlike dynamic. You can't 'leak' the stack.
  • Memory Organisation: The stack is organised in such as way that memory fragmentation will not occur. The heap offers no such assurances.
  • Memory Limits: The stack has a memory limit, the heap does not. However, the hardware has a finite amount of memory before swapping happens.
  • Resizing of Memory: It's actually possible to take an item in the heap and dynamically resize it.
NullPtr
  • Null Pointer: The null pointer is a non-valid pointer i.e. it doesn't point to any memory location.
  • Usage: Using the null pointer we can run checks before we try and deference it. This will save us crashing if it doesn't point anywhere.
  • Deletion: When we free some memory from the heap, we should always make the pointer null.

No comments:

Post a Comment