1
votes

Is there a way to know the access specifier used by the compiler in c. For Example- In the case of register variables, it all depends on the compiler to decide whether a variable's access specifier would be auto or register. Is there a way to dynamically know what access specifier is chosen by the compiler??

3
Please define dynamically. You can always compile a C module to assembly and read it. Besides, a variable may be on the stack on one moment and in a register the next. - Fred Foo
"variable may be on the stack on one moment and in a register the next"- that is why i needed to know dynamically, ie, when my program is running and the execution reaches a point where some code is written which tells me the access specifier that is used.. - ankith13

3 Answers

3
votes

Our are mixing up the specification level of the language and the realization of your program in machine code. The two terms "register" here are only loosely related.

The wording of the keyword register is just confusing, a misnomer. register only implies that you are not allowed to take the address of such a variable. Whether or not your compiler realizes a variable on the stack and addresses it directly or stores it in a CPU register is nothing stable that you can rely upon. It will change from compiler to compiler version and optimization level.

As others said you can read the assembler to know for a particular compilation if you are interested in micro-optimization, but in general it is nothing that you should even worry about.

1
votes

You could take the adress of the variable and get a hint depending on the architecture. But this approach would probably force the compiler to allocate the variable in memory instead of a register.

1
votes

Compile the C module to assembly and read that. Be aware that some compilers may perform whole-program optimization just before linking, so even the assembler output isn't 100% reliable.