I've been coding along with the series up to day 14, and though I understand ~80% of the code, there is one particular aspect of Day 0014's code that I don't get. It has to do with this line:
1 | GameMemory.TransientStorage = ((uint8 *)GameMemory.PermanentStorage + GameMemory.PermanentStorageSize); |
For reference, all related code (split across various files):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #if INTERNAL_BUILD LPVOID BaseAddress = (LPVOID)Terabytes((uint64)2); #else LPVOID BaseAddress = 0; #endif game_memory GameMemory = {}; GameMemory.PermanentStorageSize = Megabytes(64); GameMemory.TransientStorageSize = Megabytes(512); uint64 TotalMemorySize = GameMemory.PermanentStorageSize + GameMemory.TransientStorageSize; GameMemory.PermanentStorage = VirtualAlloc(BaseAddress, TotalMemorySize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); ??? -->> GameMemory.TransientStorage = ((uint8 *)GameMemory.PermanentStorage + GameMemory.PermanentStorageSize); |
and
1 2 3 4 5 6 7 8 9 | struct game_memory { bool IsInitialized; uint64 PermanentStorageSize; void* PermanentStorage; uint64 TransientStorageSize; void* TransientStorage; }; |
Here's my current understanding of the code:
We allocate a block of memory of size=permanentsize + transientsize. The struct game_memory contains a pointer to the start our permanent storage and a pointer to the start of our transient storage, it also has the sizes for both chunks of memory. In this line:
1 | GameMemory.TransientStorage = ((uint8 *)GameMemory.PermanentStorage + GameMemory.PermanentStorageSize); |
We're clearly computing the TransientStorage pointer, in other words: where our transient storage starts in the chunk of memory we allocated. However, I don't get why we're casting the PermanentStorage pointer to a uint8 pointer, I can't even begin to try and explain why we're doing that. Isn't the TransientStorage simply located at the end of the PermanentStorage? Or am I fundamentally misunderstanding what we're doing here?
Many thanks in advance for any help!