Pointer to register variables [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Address of register variable
register int yy = 6;
int * myptr = &yy;
cout<<myptr<<" "<<&yy<<" "<<*myptr<<" "<<yy<<endl;
Deitel and Deitel third edition C++ how to program : page # 307 last line says - address operator can not be applied to variables declared with the storage class register. How come the above code prints :
0x28ff24 0x28ff24 6 6
Am I missing something about pointer to variables with register storage class?

The compiler is ignoring your register request because your code takes the address of yy.

Your book is out of date.
In modern C++, the use of the register keyword is deprecated, and has no effect on the declaration (beyond perhaps acting as a hint to the compiler that the variable might be heavily used). It does not prevent you from taking the address of the variable.
C does have that restriction; and presumably older version of C++ also did, but I don't have any historical documents available to confirm that.

Duplicate of Address of register variable
Separately, here is IBMs documentation on the matter for one of their compilers:
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fregdef.htm

Related

Is there a way to force a C++ compiler to store a variable in a register? [duplicate]

This question already has answers here:
How to store a C++ variable in a register
(8 answers)
Closed 2 years ago.
I know that the inline keyword makes the compiler more likely to inline a function, although the decision is up to the compiler, and the GNU extension __attribute__((always_inline)) forces the compiler to inline it.
Correspondingly, is there...
__attribute__((always_register)) register int foo = 678;
...or something like that?
GCC has a way to specify register for a local variable through usage of keywords asm and register:
register int *foo asm ("r12");
Note the quote:
The only supported use for this feature is to specify registers for
input and output operands when calling Extended asm.
This feature is used in Linux kernel on some architectures to gain access to a thread-local storage that is stored in one of general purpose registers. This is how current macro is implemented on ARC architecture:
register struct task_struct *curr_arc asm("r25");
#define current (curr_arc)
Such usage is interesting as it uses register keyword at a global scope, while in standard C it can only be used locally.

What does mean "int(i)=1;"? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).

Why earlier versions of C made it mandatory to declare variables in the beginning? [duplicate]

This question already has answers here:
Why do the older C language specs require function-local variables to be declared up-front?
(3 answers)
Closed 7 years ago.
I have been going through a bit of history of C, and I find that in earlier versions of C, like in C89 standard, it is mandatory to declare variables at the beginning of a block.
But I also find there are some relaxations from C99 standard specification, where variable can be declared anywhere before it is used.
My question is why the earlier versions made it mandatory? my emphasis is to know if there was any technical difficulties in designing the compiler at those days, that prevented them identifying declarations at any point.
Also, with a compiler design perspective I understand, with such a restriction in C89, it is easy to handle variable declarations and usage with the help of an intermediate file to store the mappings. But are there methods that can handle the case without using an intermediary file, say some memory based storage?.
If the compiler sees a consolidated list of all the local/automatic variables up front, it can immediately work out the total amount by which to move the stack pointer to reserve stack memory for them - just one operation on the stack pointer. If it handles the variables in dribs and drabs as they're encountered in the function, moving the stack pointer incrementally, then there ends up being more opcodes dedicated to stack setup and stack pointer updates. It's important that the stack pointer be up to date whenever a further function call's performed. Newer compilers do a tiny bit of extra work to patch back in the amount by which to move the stack pointer after all the function's been considered. (I'd hazard that the effort's so minimal that the early Standard was shaped more by the conceptual appeal of knowing what to do up front than the effort of being more flexible, but if you just want to get something working - why make extra efforts?)
C99 Rationale didn't directly explain why it was not permited in C89, but did say it was added in C99 because it was permited in other languages and the it has been found useful.
Rationale for International Standard — Programming Languages — C
§6.2.4 Storage durations of objects
A new feature of C99: C89 requires all declarations in a block to occur before any statements. On the other hand, many languages similar to C (such as Algol 68 and C++) permit declarations and statements to be mixed in an arbitrary manner. This feature has been found to be useful and has been added to C99.

Register Variable Address [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Address of register variable
I know that getting address of register variable is not allowed in c . But why this code is getting compiled in c++ and not in c.
int main()
{
register int a;
printf("%u\n",&a);
}
The keyword register is only a hint to the compiler. In fact, most compilers today ignore it as they contain advanced code to pick the best register variable candidates anyway.
Whenever you take the address of a variable, it is typically placed on the stack, despite the fact that you have used the register keyword.

Declaring local arrays using non-const variables to set the length, and it works? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
why a[n] is accepted in c during runtime?
Declaring the array size with a non-constant variable
I just wrote some code to test some other code and I needed an array as input data. As the size of the input data may differ, I declared the variable as follows:
float input[num_pixels_row][num_pixels_col][3];
where num_pixels_row and num_pixels_col are non-const variables which are set using input from the command line. I ran the code and it worked.
Then after a little while I noticed what I had just done and thought "Hey, wait a minute! This shouldn't work!!" But the strange thing is that it does. Since input is declared inside a function it should be allocated on the stack, but how can the compiler determine the stack frame if the size of the array isn't known?
I asked two colleagues and they were just as puzzled. By the way, I compiled the code using g++ 4.6.1
That's a gcc-specific compiler extension which makes your code sub-standard and nonportable. For example, this won't compile in Visual C++.
Variable length arrays are a part of the C99 specification, which gcc also allows in C++ programs.
I don't think this has been added to C++11 though, unfortunately. Though I'd suspect that since many C++ compilers also strive for C compliance that they'll end up supporting this as well.