How exactly do register variables work in C? When should they be used? K&R touches upon them briefly but moves right along. Is there any source in which I can read about them in depth?
when you declare a local variable with the register keyword, you are hinting to the compiler that you would really like it if the compiler allocated the variable within a register, and never have it exist in memory. The compiler wont be able to comply if it doesn't have enough registers available to accommodate the register variable. Also, when you have declared a variable with the register keyword, you can never refer to its location in memory, as it will ideally not even be in memory, just a register. So you can't do this:
register int i;
int* a = &i;
I'm not sure if this is an error or not, but making use of &i will prevent i from being allocated in only a register. But maybe I'm wrong.
I guess I am just confused by this suggesting business. A command which may or may not do something seems odd to me.
Name:
Anonymous2011-09-25 11:04
As a rule, on modern compilers it never does anything.
You may, if you wish, use it to embed secret messages in your source code for other programmers to read. Or just use it randomly, together with auto, to confuse new programmers.
>Also, when you have declared a variable with the register keyword, you can never refer to its location in memory, as it will ideally not even be in memory, just a register. So you can't do this:
You're mixing up the concept of a declaration with a definition. Now instead of giving out some idiotic response, why don't you take the time to read one of the ANSI/ISO C standards and then write some actual C code you fucking jewish mental midget.
>>4
It may not even be possible for the compiler to satisfy your request (eg. if you declare more register variables than there are registers on your CPU). volatile is also only a hint which the compiler is free to ignore. It would make the compiler useless for real code so they don't, but it's perfectly legal for them to do so.
I don't see how a declaration or a definition are any different for a local variable.
And yet you felt the need to give out advice? WTF? Are you stupid? Are you a jew? Or are you both?
Anyways, since you clearly don't have the mental capacity to read, let me help your jewish ass out. In C, it's possible to declare a variable without actually defining it. The reason why you do this is because declared variable doesn't take up any storage. However, once you define it, it does in fact take up storage.
Name:
Anonymous2011-09-25 15:32
>>7
Also, a local variable in C doesn't imply automatic duration. Is's possible for a variable to still exist once it goes out of scope.
So then a variable definition is a declaration that allocates storage for the variable. Unless you can declare an extern variable within a function, then every variable declaration within a function is necessarily a definition. I've never seen an extern variable declared within a function, but it seems like it could actually be a good idea for using global variables from other files within the scope of a single function.
The 'register' keyword specifies a storage-class. Literally, "[it] suggests the compiler that the access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined." (ISO/IEC 9899:1999, 6.7.1, 4).
You'll almost never need to use the 'register' keyword, except in few situations which stand very far away from the average application programmer.
However, there are a several trivial cases where the object doesn't have a name.
really? I can't think of any. All that comes to mind are anonymous structs and unions, but those are just types, not objects that carry any kind of value. Do you mean objects allocated on the heap?
I think you just need to shut the fuck up something you clearly don't understand.
y-u-gata-b-so-mean-bro? If you'd like to explain where my understanding appears to fall short, I'd like to hear it though.
really? I can't think of any. All that comes to mind are anonymous structs and unions, but those are just types, not objects that carry any kind of value. Do you mean objects allocated on the heap?
First off, the heap doesn't fall within the realm of standard C. But whatever. I don't think you're ready for that. Second thing, an example of an unanamed variable would be malloc().
If you'd like to explain where my understanding appears to fall short, I'd like to hear it though.
So far you've failed to demonstrate that you understand the difference between a declaration and a definition. It also appears that you don't understand what a variable is in C. The fact that you refer to allocated objects on the heap also implies that you've never looked at one of the various C standars.
Need I go on you fucking jewish mental midget?
Name:
Anonymous2011-09-25 19:58
>>20
Also you fucking idiot, both anonymous structs and unions can be objects. Again, read one of the C standards you fucking idiot.
First off, the heap doesn't fall within the realm of standard C. But whatever. I don't think you're ready for that. Second thing, an example of an unanamed variable would be malloc(). the heap doesn't fall within the realm of standard C an example of an unanamed variable would be malloc().
Looks leik I've been trolled. Oh well.
So far you've failed to demonstrate that you understand the difference between a declaration and a definition.
What I said in >>12 was logical deduction from what was said in >>9 , so therefore, ala contradiction, >>9 must have a faulty definition for definitions and declarations.
So then a variable definition is a declaration that allocates storage for the variable. Unless you can declare an extern variable within a function, then every variable declaration within a function is necessarily a definition. I've never seen an extern variable declared within a function, but it seems like it could actually be a good idea for using global variables from other files within the scope of a single function.
A few things you hourly bitch...
a)For the most part, a variable in C is a named object. There are several trivial cases where this you can have an unnamed one.
b)It's possible to have a definition not allocate storage. Can you cite some example you illiterate jew? I bet you can't.
c)I'm still not granting you the first interview.
d)Go scrub another toilet you mental midget.
Name:
Anonymous2011-09-25 21:38
it was once when the wellknown asperger known as !!kCq+A64Losi56ze wrote:
b)It's possible to have a definition not allocate storage. Can you cite some example you illiterate jew? I bet you can't.
Well, I bet
int a[0];
would do the trick if it was allowed. I have an old programming book where they did:
int main(int argc, char** argv) {
void this_variable_has_no_usable_type_lol;
printf("And you can do this on old compilers it seems.\n");
printf("although modern ones don't seem to like it.");
return 0;
}
malloc allocates memory, like declaring (& initializing?) a variable, except without defining a name to reference/use the memory...? hence using pointers n stuff?
No, again, in C, a variable is a named object. However, an object isn't a variable.
How do these not get lost?
Objects have scope, duration, and linkage. The guy that started NetBSD told me once upon a time to think of these three things of being the property of an object. So if the object has file duration, the life of the object will persist after the function returns.
>>1
You shouldn't use register. You will only limit your possibilities doing so (de/referencing). Most modern compilers will optimize your program so that you wouldn't really spot performance differences anyway. Also, I am aware of some compilers/parsers that will ignore this keyword fully. >>27
Confirmed for TCC. This works: void emptyvar;
int emptyarray[0];
printf("A = %d, B = %d\n", emptyvar, emptyarray[0]);
...which results in undefined behaviour but without an error/warning. Also note that the two empty variables do have memory addresses.
GCC doesn't like it though.
It doesn't matter whether a given compiler supports some syntax or another. Zero-sized arrays are illegal. void-typed variables are illegal. In the latter case, a compiler that compiles such code is non-compliant, since it's strictly forbidden by ISO C99 to accept that.
Cool. I wonder what it does, like if the value of emptyvar as a memory address is even deterministic. That's a little scary.
The compiler I had that went with that old book didn't do type checking for arguments passed into functions, and it didn't check if you passed in the correct amount of arguments either. It was fun. Those errors seemed to always result in local strings getting gibberish in them.