Skip to main content
GameDev.net gamedev.net
🔒 Locked

static const vs. const in function scope

Started by janta Nov 12, 2009 at 3:27 PM 7 replies 5.3k views
Original Post
janta
janta
Hi I can't find a definitive answer to this Why would
const int [] t = { 1, 2 ,3, 4, 5 };
and
static const int [] t = { 1, 2 ,3, 4, 5 };
produce different code? In the first case, visual c++ generates code to initialize they array every time. I used to preach the "static const is redundant, const are static by default" but now I think I might be wrong, at least partially. Can someone clarify this a little bit? thank you!
DevFred
DevFred
Because in the first case, the array lives on the stack and does not stay around in memory when the function ends.
janta
janta
Well that's in fact what's I'm asking, why does the compiler need to put a const array on the stack? I'd understand if the array were not const.

Maybe just in case I'd want to const_cast and modify the array but still find the original values the next time I call the function. That is the only reason I see.
alvaro
alvaro
Well, it's a local variable. Those typically live on the stack. Whether you make it const or not doesn't change much.

In cases where the program never needs to take the address of the const variable, the compiler might be able to optimize it out and never store it anywhere. In that case, you'll probably get the same code whether the variable is static or not.
Sneftel
Sneftel
Quote:
Original post by janta
Maybe just in case I'd want to const_cast and modify the array but still find the original values the next time I call the function. That is the only reason I see.
No, that is not guaranteed to work by the Standard.
janta
janta
Quote:
Original post by Sneftel
Quote:
Original post by janta
Maybe just in case I'd want to const_cast and modify the array but still find the original values the next time I call the function. That is the only reason I see.
No, that is not guaranteed to work by the Standard.


So that means a compiler could treat the const array and the static const array in the exact same manner.
Richy2k
Richy2k
Ultimately, it's up to the compiler as to what it does. If you were to do as follows:

const int i = 27;printf( "Pre modification: %u\n", i );int* pi = const_cast< int* >( &i );*pi = 42;printf( "Post modification: %u\n", i );


The output should be:

Pre modification: 27Post modification: 27


Of course, depends on the compiler - but 'i' will typically be optimised down to '27', as 'const' is a promise to the compiler that it won't change - I tend to take advantage of this as const can allow a compiler to optimise better - if a var isn't going to change, it can make assumptions. Back to the subject though - 'static const' or 'const', depends on the compiler - whilst 'static const' tells it 'I want it to not be part of the stack', it might do something different depending on the implementation. You aren't going to modify it, so what does it matter? I will say if I only want a const var available in a function, it stays const. If it needs to be kept out of the function, it goes global or member. Static generally has no benefit - again, depends on compiler, unless of course you are doing low level optimisation, and the assembly output shows something different to what you want. Generally you want to avoid thinking low level until it truly matters [smile]
Sneftel
Sneftel
Quote:
Original post by janta
So that means a compiler could treat the const array and the static const array in the exact same manner.

Not always. Consider the following:

struct Foo {    Foo() { std::cout << "Foo!\n"; }};void bar() {    static const Foo f;}int main() {    bar();    bar();    bar();}


The constructor method is printed only once, versus thrice for the non-static version.
Sneftel
Sneftel
Quote:
Original post by Richy2k
The output should be:

Pre modification: 27Post modification: 27
Also not quite right. The results of modifying a variable originally declared as const are undefined. The computer could print 27; it could print 42; it could print -8.2; it could beep and then catch fire.

Topic Locked

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

Sign in to reply to this topic.