I'm sorry to ask what is probably such a stupid question, but I am very new to all this!
I am wondering what the rules around modifying global variables in code are.
I have set up a synthetic test which declares a string in global space like this:
Input.c:
Code: Select all
char variable[] = "blah";
Code: Select all
void Update()
{
if (joystate & BUTTON_A)
{
variable[2] = 'a';
variable[3] = 'h';
}
}
Code: Select all
VDP_drawTextBG(PLAN_A, variable, 1, 15);
The string updates on the screen. But I was under the impression that all of the code is stored in ROM, and would therefore not be modifiable?
Do I need to declare this variable in RAM by calling malloc() and setting the returned contents to the string I want for modifyable variables? Or is it ok to modify any global variable once the game starts, and it be ok as in my test?
The reason I am asking is because I am exporting sprite definitions into .c files and want to export most of it's data in tact, but want to set function pointers into them at runtime. Is that possible? Or do I need to malloc the size of memory needed to hold the sprite definition, set the data into that malloced memory and go from there?
I would really like to just set the data into the variable directly without mallocing everything but want to make sure I am doing the right thing before everything explodes!
Thanks guys.