Handmade Hero»Forums»Code
The_8th_mage
69 posts / 1 project
local strings problem
I am fairly new to c++, having worked on c# for at least 5 years. i tried making a function that returned a string using the method casey used, which allocated all the memory on a single call in the beginning of the project.
with this constraint, i have 2 options:
a. using char s[something], but then returning it to the main problem is problematic because it's local and the memory may be changed on function calls.
b. allocating it into the memory arena, but then i can't delete it without using a clever scheme for the arena.
right now i allocate it and store the string for eternity on the gameState struct, but i think it's kinda hacky. i hope someone can show me a new way to handle it.
thanks.
Mārtiņš Možeiko
2358 posts / 2 projects
local strings problem
Well the first question should be why do you need to allocate persistent string at all?

If you really really do, an not for duration of whole project, divide memory arena into some smaller temporary storage. And allocate strings (or anything else) inside this temporary storage. Once you're done with memory in it (end of frame, end of game level, etc..) free all the temporary storage which should be done by simply setting UsedBytes integer to 0.
The_8th_mage
69 posts / 1 project
local strings problem
that sounds kinda good.
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
local strings problem
In games it's pretty typical for nearly all strings to fall into two broad categories, both of which are trivial to handle. The first are ones that can be preprocessed down to integers prior to run-time, and the second are ones that are transient inside a frame and can be continually vaporized in the manner already described above.

Occasionally there is a real need for actually persistent strings, but it is so rare that you can usually just do something one-off for them and don't really have to think particularly hard about it, or even just used fixed 256-byte chunks for them or other things that you wouldn't probably want to do if you were trying to make a system that really revolved around extensive string handling.

- Casey