0

When you declare a variable with register attribute in C, how can you find out which CPU register it is stored in?

register int i;
  • 6
    you don't because the register keyword doesn't guarantee it will even be in a register, it is just a hint to the compiler. – Michael Petch Feb 25 '18 at 03:15
  • How would you do it in case it really goes into a register? –  Feb 25 '18 at 03:19
  • 5
    You can read the assembly that the compiler outputs. – afic Feb 25 '18 at 03:23
  • 4
    Read the asm the compiler emitted to see what it did. `gcc -O3 -S -fverbose-asm` can be useful; it adds comments on each line naming the operands. If `i` wasn't optimized away, or the uses of it transformed into something else, then it may show up in the comments. https://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output, and also https://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc – Peter Cordes Feb 25 '18 at 03:24
  • Even if you don't use `register` the variable could still be stored in a register. Generally it is best to leave this to the compiler, which will probably decide this itself anyway (even if you add `register` to some variables). – Bo Persson Feb 25 '18 at 09:17
  • 1
    @Tuhnri Note that with modern SSA-based compilers, there isn't really a single register the variable is hold in. Rather, the compile translates each variable into many single-use variables and assigns a register to each of them. It's not uncommon to see a variable change registers all the time in assembly. – fuz Feb 25 '18 at 11:34

0 Answers0