|
Second week the programming course, this week the lectures been about arrays, pointers and how to handle RAM, and this is something I’ve had some trouble getting my head around in the past, pointers and memory allocation that is. I’ve worked with pointers and the new- and delete operators, in the past but never really understood how they work, how I use them or why I should use them.
So if I understood everything right, this is what I’ve learned:
Every time a variable, string or texture is created/used in a program, it is stored in the computer’s Random Access Memory which consists of different memory pools, and we use the stack and the heap pool.
The stack memory pool holds about 1 MB of memory, and is the memory that is available by default in Visual Studios, and when the main function is ended, all of the memory from the stack will be deleted, so that it can be used again. 1 MB isn’t a huge amount of memory, so in order to access and use more memory we need to allocate memory from the heap memory pool. The heap pool contains a whole lot more memory, and we’re gonna need a lot more RAM than 1MB for a game, so the use of the heap memory is required. Unlike the stack pool, the heap pool does not delete the memory that we’ve allocated, so we need to use the delete operator in order to prevent memory leakage. If we don’t delete the allocated memory, there will be unusable RAM in the heap pool. Memory leakage is often the reason why a game or program crashes, so it’s extremely important to minimize memory leakage.
Also, we’ve started working with the SDL library, which is a library that enables simple implementation of sprites and sound in Visual Studio. Our teacher showed us how to make the Pong game. I have worked with a similar library before, the SFML library which I think is much simpler than SDL, and apparently we’ll work in SFML next year, but there is probably a good reason why we’re working in SDL now instead of SFML. Either way, now I’ve recreated Pong!!! Yay!

About Johan Holmér
2014 Programming
|