I read that gcc provides support to define global variables as register stored variables. What I want to know is that do the standards have any specifications for this support.
- 133,132
- 16
- 183
- 261
- 1,379
- 11
- 29
-
2You might also want to [read this](http://stackoverflow.com/a/315035/314291) – StuartLC Jul 24 '15 at 09:57
5 Answers
There is a common misconsception that C's keyword register talks about hardware registers. That might be the origin of that concept, but in modern C this is not the purpose. The only real effect that register has, is that the & is not allowed on such a beast.
They may be realized in any way the compiler wants, hardware registers, instruction immediates, or on the stack, you wouldn't know. What you do know is that a register variable can't alias with other variables.
And to answer your question more directly, no register in file scope is not part of the C language. If it would, that would allow us to declare register const variables of almost any base, that could serve as some sort of global constants.
Mapping hardware register to specific variables is an extension that compilers provide, e.g gcc. Gcc's feature, as an extension, also works in file scope. But that is quite prohibitive, since usually CPU's don't have many hardware registers to spare.
- 76,821
- 6
- 102
- 177
register may not be used for global variables. This is covered by C11 6.9/2:
Constraints
The storage-class specifiers
autoandregistershall not appear in the declaration specifiers in an external declaration.
Here, external declaration means any declaration that is not within a function. (Not to be confused with extern, or external linkage).
- 138,810
- 21
- 208
- 365
First and foremost. allocation of "register stored variables", if any, are the job of compiler. C standard does not specify anything mandatory about the same.
That said,
A global variable cannot be defined with
registerstorage class, as perC11, chapter §6.9The storage-class specifiers
autoandregistershall not appear in the declaration specifiers in an external declaration.Use of
registerdoes not guarantee the allocation in register, Chapter §6.7.1 (emphasis mine)A declaration of an identifier for an object with storage-class specifier
registersuggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.
- 1
- 1
- 133,132
- 16
- 183
- 261
-
I wonder why the Standard forbade the use of a register qualifier with global variables, since such a thing could improve optimization even without global register allocation. Given `extern int foo; extern int *bar;` the code `foo++; *bar ^= 1; foo++; *bar ^= 2;` must perform all four steps in order. If `foo` could be register-qualified, the operations could be reordered as `foo+=2; *bar^=3;`. – supercat Jan 28 '16 at 17:47
Global register variables are not supported or permitted by C99 or C11 standards (see Sourav Ghosh's or Matt McNabb's answers).
As an extension, GCC accepts
register int *foo asm ("a5");
to declare a global int variable sitting in register a5... This is very rarely useful, and you need to understand how GCC generates code and allocates registesr, what are the calling conventions of your ABI, ... to be able to use that without fears... BTW, it probably weaken GCC optimization abilities.
But it it is GCC specific, Clang/LLVM does not support that extension, even if it is supporting several other GCC extensions (e.g. computed goto-s, ...).
- 1
- 1
- 223,805
- 18
- 296
- 547
-
1On the ARM, if a program which need a small number of global variables that are used a *lot*, putting all such variables into a struct and dedicating a register to holding their address will eliminate the need to load an indexing register when accessing any of those variables. Not common on larger embedded systems, perhaps, but sometimes very useful on smaller ones. – supercat Jul 30 '15 at 17:53
No. The standards do not specify any feature like this. The storage class register behaves like auto and cannot be used for global variables.
As a rule of thumb, everything that is intrinsic to a certain machine is not part of standard C.
- 88,405
- 25
- 200
- 352