2

K&R 说

编译器可以随意忽略建议(注册声明)。

在什么情况下,如果我定义 gcc 会忽略register int x = 4;

4

3 回答 3

4

这完全取决于实现。

一般来说,您应该相信编译器会将变量放入寄存器,而不是自己定义它们。

C99 6.7.1 存储类说明符

使用存储类说明符为对象声明标识符register 建议尽可能快地访问该对象。此类建议的有效程度由实施定义。

另外,C++11 已经弃用register关键字作为存储类说明符,也许将来某个时候 C 也会这样做。

于 2013-09-13T12:13:12.430 回答
2
于 2013-09-13T12:15:49.487 回答
1

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(&).

于 2013-09-13T12:23:47.800 回答