Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
1  
See this SO question: stackoverflow.com/questions/15908835/c-c-register-variable. C++ treats register as a hint and may ignore it; C can do the same, although compilers like GCC will complain if you go after the address of a variable declared as a register. –  Blrfl Jul 9 '13 at 22:43
    
@Blrfl: I don't think that answers my question! Moreover I have found something interesting, I'll edit my question, have a look. –  stalin Jul 9 '13 at 22:56
2  
A register has no address. It is not part of main memory. –  Steven Burnap Jul 10 '13 at 0:16
1  
-1: pet peeve of mine: Trying 'it' on 'your' compiler is no substitute to reading the language specification, and a very poor way to definitively understand what the language construct should do. –  mattnz Jul 10 '13 at 3:45
1  
+1 for asking clarification after you saw results you couldn't understand. –  Bart van Ingen Schenau Jul 10 '13 at 10:30
show 3 more comments

1 Answer

the variables of a program (on modern non-embedded architectures) can live in one of 2 places, the RAM and the registers. the RAM is indexed and registers can be accessed directly in the opcodes

the address of a variable is the index where it lives in the RAM, a register doesn't live in the RAM so there is no way to create a meaningful address for it unless you put it on the stack but that defeats the purpose of the register keyword

as fun as the keyword is, these days compilers are good enough to decide for themselves which variables never need to get copied on the stack or not (the sub branch there is called register allocation)

and as with most old school optimizations giving hints to the optimizer is more likely to confuse and/or hinder it if you don't know exactly what the results are, because optimizers are geared towards the idiomatic language constructs, and unless there is a bottle neck in the function don't worry about it. don't shave off 1 or 2 cycles of a rarely used function when the true bottle neck is the DB access.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.