简体   繁体   中英

cases for gcc to ignore register declaration?

K&R says

compilers are free to ignore the advice (register declaration).

In what cases, gcc would ignore if I define register int x = 4; ?

This is totally implementation-dependent.

In general, you should trust the compiler to put variables to register and not define them yourself.

C99 6.7.1 Storage-class specifiers

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

Plus, C++11 has deprecated the use of the register keyword as a storage-class-specifier, maybe sometime in the future C will do the same.

By declaring a variable as 'register' , we are only requesting (not forcing) the compiler to store variable a in CPU register. Compiler will decide where to store the variable.

If you have declared more variables as register , then few of them might get into the CPU register as the CPU registers are limited. That is again implementation dependent.

If we refer to the C99 draft standard we see that this is implementation defined, section 6.7.1 Storage-class specifiers paragraph 4 says:

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined .

This thread from gcc mailing list seems to indicate if you follow the thread a bit that register has been ignored for a while except in the case that you do not use optimization( using -O0 ).

Note that using register does have another impact in that it prevents the use of address operator( & ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM