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

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

问题描述

我知道寄存器变量的概念和它的使用案例,但也有基于什么我试图在我的脑海几个问题。


  1. 我无法访问在C寄存器变量的地址,但我能做到这一点的C ++!为什么?是否有访问寄存器变量的地址?

  2. 的任何问题
  3. 假设,如果我用C声明一个字符串变量++为寄存器,然后在那里将变量存储?什么是声明存储类非数字数据类型,如C'串'++是注册??


更新:
我认为C ++允许我们取一个寄存器变量的地址,因为我没有得到我的程序中的任何错误是如下:

 #包括LT&;&iostream的GT;
#包括LT&;&time.h中GT;使用命名空间std;clock_t表示求,连接;诠释主(){    INT J,K = 0;    求=时钟();
    对于(寄存器INT I = 0; I<千万,我++){
        / *如果(K == 0){
            COUT<<&安培; I<< ENDL; //如果code是取消注释,那么C ++拒绝的建议,使我为寄存器
            ķ++;
        } * /
    }
    EN =时钟();    COUT<< EN-求<< ENDL;    COUT<<&放大器; J<<&ENDL LT;<&安培; k;    返回0;
}

我所观察到的是,如果我把这些变量'我'为寄存器,不要尝试使用的地址打印'和;我',那么C ++接受这项建议,并存储在寄存器'我',这可能是从运行的时间循环,这将始终是围绕4-12毫秒,如果'我'是寄存器infered。但是,如果我尝试打印变量i的话,虽然我没有得到任何错误,但C ++拒绝该建议,这可以从循环的执行时间,这始终是超过25,如果我没有登记被infered地址! !

所以,基本上我不能存储类取一个变量的地址在C寄存器以及C ++!为什么?


解决方案

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


  • 在C,你不能把一个变量与地址寄存器存储。参看C11 6.7.1 / 6:


      

    的标识符与存储类说明的对象的声明注册
      建议访问对象尽可能快。到这样的程度
      建议是有效的是实现定义的。


      
      

    脚注:实现可能只是将任何注册声明为汽车声明。 [...]



  • 在C ++中,注册是去precated,有没有效果(也许除了作为一个编译器提示)无意义的关键字,和变量声明为注册仍然只是具有自动存储。特别是,C ++的没有的一个注册存储类。 (它只是存储类别的说明的,从C继承)参看C ++ 11 7.1.1 / 3:


      

    A 注册符是一个提示,这么声明的变量将被大量使用的实施。 [注:该提示可以忽略,而且在大多数实现中,如果变量的地址取它会被忽略。这种使用德precated [...]



即使在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. 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?

  2. 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??

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;
}

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!!

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

解决方案

C and C++ are different languages.

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

    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.

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

  • 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:

    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 [...]

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天全站免登陆