Skip to main content
gamedev.net
🔒 Locked

Hardcoded arrays?

Started by Axiverse 5 replies 650+ views
Original Post
Axiverse
Axiverse
can you make hardcoded arrays? For ex. I want to make a graphics engine and i want a conversion table for various enumerations, and i think the easiest way would be through a array, but I don't want to wast the time to intilize the array, so can I hardcode it?
Ra
Ra
Sure.

int my_int_array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Ra
Axiverse
Axiverse
Isn't that still a intilized global variable?

If it's the best there is... i guess it'll have to do.. =)
ajas95
ajas95
well, if you make the array definition local to a function, then the array will have to be initialized on the stack each time you make the function call (in most cases). Or, you could define the array globally/statically and it will only be initialized once... but then the cache miss to access the static memory might cost as much as the time spent initializing the array.

So it depends :)

The best case is if for each compilation unit, the compiler can resolve the value at compile time. What conversions for enumerations exactly are you trying to create?
MaulingMonkey
MaulingMonkey
Quote:
Original post by Axiverse
Isn't that still a intilized global variable?

If it's the best there is... i guess it'll have to do.. =)


Even a "hardcoded" array has to be initialized - if you mean the difference between "setting them at run time" and "setting them at compile time" - well, the values of my_int_array will be read from the EXE just as a const int would.

That said, if using classes with non-trivial initialization semantics, those will be run at run-time.

Adding the const keyword will allow the compiler to replace "my_int_array[5]" with 4 throughout the code where this is advantageous, taking advantage of the fact that my_int_array[5] can't change in value (unless you add the volatile keyword as well - which is legal - meaning: "The users of this code can't modify the value, but it can be changed externally".
Qw3r7yU10p!
Qw3r7yU10p!
How much time do you think you'll be able to save?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.