1

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??

ankith13
  • 371
  • 1
  • 3
  • 10
  • 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 Aug 09 '11 at 09:29
  • "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 Aug 09 '11 at 09:36

3 Answers3

3

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.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I can understand the usage of the register variable...it is also well explained in other posts... but I just wanted to know if there is a mechanism to know the access specifier which is being used... an api or any asm code or something... i am sorry if I am offending you.. and thank you for ur explanation.. – ankith13 Aug 09 '11 at 14:05
1

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.

henkebenke
  • 78
  • 6
  • This will indeed force the variable to be stored in/copied to main memory, since you can't take the address of a register (at least not on the x86). – Fred Foo Aug 09 '11 at 09:38
  • According to the tags, he's asking about C. In C you're simply not allowed to take the address of an object declared with the `register` storage class (§6.5.3.2/1). Your answer would be more accurate about C++. – Jerry Coffin Aug 09 '11 at 18:37
1

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • That is a good idea, I have tried it but is there a way to know it in a program, by calling an api or by any other method... – ankith13 Aug 09 '11 at 09:43
  • also as you specified this method is not 100% reliable... is there anything else that can be done.. – ankith13 Aug 09 '11 at 09:44
  • Not in standard C, in any case. You can turn whole-program optimization off (or use a different compiler) to make this reliable. – Fred Foo Aug 09 '11 at 09:45