C和C++中寄存器变量的地址 [英] address of register variable in C and C++

查看:32
本文介绍了C和C++中寄存器变量的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道寄存器变量的概念及其用例,但根据我的尝试,我的脑海中几乎没有问题.

I know the concept of register variable and it's use cases but there are few questions in my mind based on what I have tried.

  1. 虽然我可以在 C++ 中访问,但我无法访问 C 中的寄存器变量的地址!为什么?访问寄存器变量的地址有什么问题吗?

  1. I cannot access the address of a register variable in C though I can do it C++! Why? Is there any issue in accessing the addressing of a register variable?

假设如果我在 C++ 中将一个字符串变量声明为寄存器,那么该变量将存储在哪里?将C++中的'string'等非数字数据类型的存储类声明为register有什么意义?

Suppose if I declare a string variable in C++ as register, then where will that variable be stored? What is the point in declaring the storage class of non-numeric data types such as 'string' in C++ to be register??

更新:我认为 C++ 允许我们获取寄存器变量的地址,因为我的程序中没有出现任何错误,如下所示:

UPDATE: I thought that C++ allows us to fetch the address of a register variable, as I was not getting any error in my program which is as follows:

#include<iostream>
#include<time.h>

using namespace std;

clock_t beg, en;

int main(){

    int j, k=0;

    beg=clock();
    for(register int i=0;i<10000000;i++){
        /*if(k==0){
            cout<<&i<<endl;    // if this code is uncommented, then C++ rejects the recommendation to make 'i' as register
            k++;
        }*/
    }
    en=clock();

    cout<<en-beg<<endl;

    cout<<&j<<endl<<&k;

    return 0;
}

我观察到的是,如果我将变量i"作为寄存器并且不尝试使用&i"打印地址,那么 C++ 接受建议并将i"存储在寄存器中,这可以是从 for 循环的运行时间推断,如果i"在寄存器中,它将始终在 4-12 毫秒左右.但是,如果我尝试打印变量i"的地址,那么虽然我没有收到任何错误,但 C++ 拒绝了建议,这可以从执行循环的时间推断出来,如果 i 没有注册,则该时间总是超过 25!!

What I have observed is, if I make the variable 'i' as register and don't try to print the address using '&i' then C++ accepts the recommendation and stores 'i' in register, this can be infered from running time of for loop which will always be around 4-12 ms if 'i' is in register. But if I try to print address of variable 'i' then though I don't get any error but C++ rejects the recommendation and this can be infered from the time of execution of loop which is always more than 25 if i is not register!!

所以,基本上我无法获取具有存储类的变量的地址作为 C 和 C++ 中的寄存器!为什么?

So, basically I cannot fetch address of a variable with storage class as register in both C as well as C++!! WHY?

推荐答案

C 和 C++ 是不同的语言.

C and C++ are different languages.

  • 在 C 中,您不能使用 register 存储来获取变量的地址.参照.C11 6.7.1/6:

  • In C, you cannot take the address of a variable with register storage. Cf. C11 6.7.1/6:

带有存储类说明符register的对象标识符声明建议尽可能快地访问对象.这种程度建议是否有效由实现定义.

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.

脚注:实现可以将任何 register 声明简单地视为 auto 声明.[...]

Footnote: The implementation may treat any register declaration simply as an auto declaration. [...]

  • 在 C++ 中,register 是一个已弃用、无意义的关键字,没有任何作用(可能用作编译器提示除外),并且声明为 register 的变量仍然只是有自动存储.特别是,C++没有注册"存储类.(它只有存储类说明符,继承自C.)参见.C++11、7.1.1/3:

  • In C++, register is a deprecated, meaningless keyword that has no effect (except perhaps serve as a compiler hint), and variables declared as register still just have automatic storage. In particular, C++ doesn't have a "register" storage class. (It just has the storage class specifier, inherited from C.) Cf. C++11, 7.1.1/3:

    register 说明符是对实现的提示,即如此声明的变量将被大量使用.[ 注意:提示可以被忽略,并且在大多数实现中,如果变量的地址被获取,它将被忽略.这种用法已被弃用 [...]

    A register specifier is a hint to the implementation that the variable so declared will be heavily used. [ Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated [...]

  • 即使在 C 中,实际上也无法保证寄存器存储的实现方式(实现可以自由地将 register 视为 auto),但无论如何都适用语言规则.

    Even in C nothing is actually guaranteed about how register storage is implemented (implementations are free to treat register as auto), but the language rules apply regardless.

    这篇关于C和C++中寄存器变量的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆