Register Variable Address [duplicate] - c++

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.

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.

Why does C++ compiler allows to create an array of unknown size on stack at compile time? [duplicate]

This question already has answers here:
Disable variable-length automatic arrays in gcc
(2 answers)
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 4 years ago.
I try to understand, what happens when creating an array of unknown size on stack at compile time. Let consider this code:
int main()
{
int x;
cin >> x;
int tab[x];
}
I found a lot of information about this saying that you can not create an array of unknown size on the stack, but I didn't find any information why does the C++ compiler allows it, or maybe some of them do? What happens when creating such array? Is it even created on stack or already on heap?
Does the GCC compiler have some option to turn on, thanks to which such a constructions would be considered as errors or at least warnings?
C++ does not permit Variable Length Arrays (VLAs).
However, the most recent C standard does, so it can sometimes be found as an extension, such as it is with the GCC.
When compiling, make sure so explicitly select a language (chose C++17 or later if you can) and ask for pedantic (strictly standards-conforming) behavior.

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).

What does (void)variableName mean [duplicate]

This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.

Pointer to register variables [duplicate]

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