Could someone please explain the difference in how the 2 snippets of code are handled below? They definitely compile to different assembly code, but I'm trying to understand how the code might act differently. I understand that string literals are thrown into read only memory and are effectively static, but how does that differ from the explicit static below?
struct Obj1
{
void Foo()
{
const char* str( "hello" );
}
};
and
struct Obj2
{
void Foo()
{
static const char* str( "hello" );
}
};
The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).
const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that . c file and cannot be seen outside of it.
const char* is a pointer to a constant char, meaning the char in question can't be modified. char* const is a constant pointer to a char, meaning the char can be modified, but the pointer can not (e.g. you can't make it point somewhere else).
const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.
With your static version there will be only one variable which will be stored somewhere and whenever the function is executed the exact same variable will be used. Even for recursive calls.
The non-static version will be stored on the stack for every function call, and destroyed after each.
Now your example is a bit complicated in regards to what the compiler actually does so let's look at a simpler case first:
void foo() {
static long i = 4;
--i;
printf("%l\n", i);
}
And then a main something like this:
int main() {
foo();
foo();
return 0;
}
will print
3
2
whereas with
void foo() {
long i = 4;
--i;
printf("%l\n", i);
}
it will print
3
3
Now with your example you have a const, so the value can't be changed so the compiler might play some tricks, while it often has no effect on the code generated, but helps the compiler to detect mistakes. And then you have a pointer, and mind that the static has effects on the pointer itself, not on the value it points to. So the string "hello" from your example will most likely be placed in the .data segment of your binary, and just once and live as long as the program lives,independent from the static thing .
A local static variable is initialized the first time its definition is encountered, but not destructed when the function exits. So it keeps its value between invocations of the function.
In case of a const
this is not all that helpful - at least, as long as constructing the constant value is as neglectable performance-wise as the assignment of an address. (If the const
object is not a constant expression, or the expression takes considerable resources to create - like in const Foo bar = foobar();
, where foobar()
can take considerable time -, the difference might become important.)
Where it does make a difference is when you want to return the object per reference or pointer: You cannot return a reference or pointer to a local object, unless it is a local static object. (Thanks to Matthieu for pointing this out.) However, when you want to use this you need to keep in mind that local statics are inherently thread-unsafe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With