To summarize what it sounds like you already understand, we allocated a large block of memory big enough for both TransientStorage and PermanentStorage. GameMemory.PermanentStorage points to the start of this block and we want GameMemory.TransientStorage to point to GameMemory.PermanentStorageSize bytes after it.
It seems like the obvious way to do this would be simply
| GameMemory.TransientStorage = GameMemory.PermanentStorage + GameMemory.PermanentStorageSize;
|
However, we have to understand how pointer arithmetic works in C. When we add X to a pointer, the compiler doesn't just add X bytes to the pointer's location. It adds X * the size of whatever the pointer is pointing to.
So when this code is compiled and run
| short* ptr_a = (short*)10;
short* ptr_b = ptr_a + 2;
|
ptr_b will be set to 14, as a short is 2 bytes big.
Now we have a problem - GameMemory.PermanentStorage is of type void*. void* means "This doesn't point to a specific type". void doesn't have a size, so you can't do pointer arithmetic on it. If I change the shorts above to void and actually try to compile in Visual Studio I get this error.
| error C2036: 'void *' : unknown size
|
And indeed, looking back at this line
| GameMemory.TransientStorage = GameMemory.PermanentStorage + GameMemory.PermanentStorageSize;
|
If we just tried to compile as is, we would get the same error. Now because we want to go PermanentStorageSize bytes forward, we can cast PermanentStorage to a type that is 1 byte big, like uint8, and then add to it. So we get the final line
| GameMemory.TransientStorage = ((uint8 *)GameMemory.PermanentStorage + GameMemory.PermanentStorageSize);
|
Hope that helps!