我知道 inline
关键字使编译器更有可能内联函数,尽管决定取决于编译器,并且 GNU 扩展 __attribute__((always_inline))
强制编译器内联它。
相应的,有没有...
__attribute__((always_register)) register int foo = 678;
...或者类似的东西?
最佳答案
GCC 有一种方法可以通过使用关键字 asm
and register
来指定局部变量的寄存器:
register int *foo asm ("r12");
注意引用:
The only supported use for this feature is to specify registers for input and output operands when calling Extended
asm
.
某些架构上的 Linux 内核使用此功能来访问存储在通用寄存器之一中的线程本地存储。这就是 current
宏在 ARC architecture 上的实现方式:
register struct task_struct *curr_arc asm("r25");
#define current (curr_arc)
这种用法很有趣,因为它在全局范围内使用 register
关键字,而在标准 C 中它只能在本地使用。
关于c++ - 有没有办法强制 C++ 编译器将变量存储在寄存器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61874935/