[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] Programming style



Andreas Schwab wrote:
Static variables at block scope work exactly like static variables at
file scope except for visibility.  There is no such hidden code.

Sorry, I should have tried it first, but I had no compiler here... Fortunately my cross compiler can be easily installed :-)

I actually messed up between C and C++.

In C, global/static variables must be initialized with constant expressions. In this case, it doesn't matter when they are initialized, so in practice they are initialized before startup, in the DATA segment.
So Andreas, you are totally right.
And Jo Even, keep your variable inside the function, there is absolutely no drawback.

However, in C++, things are different.
In case of global/static variables initialized with constants, things are the same as in C. But in C++, global and static variables can be initialized with a variable expression, for example a function result.

In case of global variables initialized with a variable expression, they are initialized before main().

And in case of static variables declared inside a function and initialized with a variable expression, they are initialized the first time they are called, using a hidden if() and hidden additional boolean variable as I described in my previous message.

Sorry to have confused C and C++, and to have answered before having double checked.
Many thanks to Andreas for checking that we don't write wrong things.

--
Vincent Rivière