有没有办法强制C++编译器将变量存储在寄存器中?
Is there a way to force a C++ compiler to store a variable in a register?
我知道inline
关键字使编译器更有可能内联函数,尽管决定取决于编译器,而 GNU 扩展__attribute__((always_inline))
强制编译器内联它。
相应地,有没有...
__attribute__((always_register)) register int foo = 678;
。或类似的东西?
GCC 有一种方法通过使用关键字asm
和register
来指定局部变量的寄存器:
register int *foo asm ("r12");
请注意引用:
此功能唯一支持的用途是指定寄存器 调用扩展
asm
时的输入和输出操作数。
此功能在某些体系结构的 Linux 内核中使用,以访问存储在某个通用寄存器中的线程本地存储。这就是宏在 ARC 架构上的实现current
方式:
register struct task_struct *curr_arc asm("r25");
#define current (curr_arc)
这种用法很有趣,因为它在全局范围内使用register
关键字,而在标准 C 中,它只能在本地使用。
相关文章:
- 存储变量的更有效方法是什么?
- 使用 system() 不存储变量C++?
- 在递归DP中,通过存储变量来分解递归调用:效率低下
- 使用 LDAP 存储C++变量
- 在满足特定条件时运行迭代C++以存储变量
- 使用getline解析和存储变量
- 如何存储变量的地址并使用指针打印值
- C++ / Objective-C++ - 如何在 NSDictionary 中存储C++变量?
- 如何从文件中读取并存储变量
- 指针是否延长了自动存储变量的生存期
- 如何在 INI、CFG 或 XML 文件中存储C++变量
- 如何在 c++ 中的字符串数组中存储变量
- 如何在特定偏移量处存储变量
- 如何存储变量的前一个值并更新它以进行比较
- 存储变量的有效方法是什么?(自制虚拟机)
- 在C++中存储变量
- 在c++中按值调用时,存储变量的副本
- 为什么lambda只捕获自动存储变量
- 如何在寄存器中存储c++变量
- Getline()结合cin.ignore()不存储变量?(错误)