#4579 - Indecisive - Thu Apr 03, 2003 8:03 pm
Okay, everything I know about C programming (not much) has come from GBA developing. So I have a question about how compilers work.
If I include a header with a lot of definitions in it, but then only use some of those definitions, do all those definitions still end up in the final code after I compile it?
I'm trying to keep my program size down, so would it be better if I made a custom header for each program which only defines those things I actually use?
#4582 - siaspete - Thu Apr 03, 2003 8:51 pm
Types generally don't take any space. They're not code as such, just information about how memory is laid out.
Functions and global (or static) variables can take up space.
#4584 - Indecisive - Thu Apr 03, 2003 9:17 pm
Thanks for the explanation. About static variables though...
If static variables take up space, then instead of defining values that I refer to often, eg. maximum ammo capacity, it's better (memorywise) to just type the number each time? For values that don't change at all?
Or are static variables something else entirely?
#4591 - tepples - Fri Apr 04, 2003 12:51 am
Indecisive wrote: |
If static variables take up space, then instead of defining values that I refer to often, eg. maximum ammo capacity, it's better (memorywise) to just type the number each time? For values that don't change at all? |
These aren't static variables; they're constants. Depending on how they're defined, what language (C or C++) you're using, and even (on ARM or Thumb) how large they are, constants may or may not take up extra space.
Quote: |
Or are static variables something else entirely? |
"Static" variables and functions in file scope are visible only within the file.
"Static" variables in function scope are local to the function and preserve their value from call to call.
"Static" fields in a class are singletons: the field belongs to the class and not to each object.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#4615 - Indecisive - Fri Apr 04, 2003 9:49 pm
Okay. Thanks for the help.
#4621 - Sweex - Sat Apr 05, 2003 2:45 am
I wouldn't worry about the size of the executable (elf) too much if I were you. Although the GBA has limited resources, a few bytes more or less isn't gonna be a big difference.
#defines are replaced with their actual value before the file is actually compiled.
As for variables. Make them constant, so they'll be put into ROM instead of RAM.
#4699 - Quirky - Mon Apr 07, 2003 7:37 am
Sweex wrote: |
As for variables. Make them constant, so they'll be put into ROM instead of RAM. |
A variable in Read Only Memory?! Not a very good idea, I would have though.
Variables, which vary, should be stored in RAM - IWRAM (default) if you are going to make the binary multiboot, or EWRAM if it is a large chunk of memory and will change - other parameters that can be considered data and are not going to be changed at run time are OK to go in ROM.
Apologies if you knew that already, but better to make it clear than chase silly bugs later. With the visoly flash card you can actually write to ROM, but it is not a safe tactic and may not work on other cartridges.