Sign in to follow this  
Axiverse

Hardcoded arrays?

Recommended Posts

Axiverse    366
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?

Share this post


Link to post
Share on other sites
ajas95    767
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Ra
Sure.


int my_int_array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Don't you mean const int? ;)

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
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".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this