Does compiler include global variables without static modifier in the global symbol table?

222 views Asked by At

I've read in (Effective Objective-C 2.0) that static variables declared in implementation file (m.file) are local to the translation unit in which they are defined and these variables will not be exposed in the global symbol table. But if a global varible in .m-file is declared without static it acts like a static. So is static used implicitly in such case or these are different matters?

Example:

//in the m.file

static int staticVariable;//100% static
int globalVariable;//is static ?
@implementation SomeClass {
//local ivars declaration
}
1

There are 1 answers

0
Thomas Matthews On

Placing variables is compiler dependent.

The compiler is allowed to place file static variables into the global variable segment, as long as the scoping rules are adhered to. Actually, the compiler can place the variables anywhere in read-write memory, again as long as the "as-if" rule is adhered to.

When defining variables at the file scope, the difference between using static and not, is that static hides the variable from other translation units. Otherwise they are treated the same.

Note: This applies to C++ since the OP has the C++ tag. I don't know if this also applies to Objective C since that is another language.