Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Register Variables

Name: Anonymous 2011-09-25 10:47

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?

Name: Anonymous 2011-09-25 10:50

yes, try google

Name: Anonymous 2011-09-25 10:55

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.

Name: Anonymous 2011-09-25 10:58

Thank you.

I guess I am just confused by this suggesting business. A command which may or may not do something seems odd to me.

Name: Anonymous 2011-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.

Name: Anonymous 2011-09-25 12:59

>>3

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

Name: Anonymous 2011-09-25 13:23

>>6

I don't see how a declaration or a definition are any different for a local variable.

Name: Anonymous 2011-09-25 13:51

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

Name: Anonymous 2011-09-25 15:28

>>7

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: Anonymous 2011-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.

Name: Anonymous 2011-09-25 15:38

>>6
kill yourself you fucking faggot

Name: Anonymous 2011-09-25 15:53

>>9

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.

>>10

yes.

Name: sage 2011-09-25 16:01

>>3

It's an error to attempt to take the address of a variable with storage-class "register". Read the standard.

Name: Anonymous 2011-09-25 16:09

>>1

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.

Name: Anonymous 2011-09-25 16:11

>>13

Thanks for clarifying register friend.

Name: Anonymous 2011-09-25 18:59

>>12

So then a variable definition is a declaration that allocates storage for the variable

Informally a variable in C is a named object. However, there are a several trivial cases where the object doesn't have a name. 

Unless you can declare an extern variable within a function, then every variable declaration within a function is necessarily a definition

I think you just need to shut the fuck up something you clearly don't understand.

Name: Anonymous 2011-09-25 19:00

>>12

So then a variable definition is a declaration that allocates storage for the variable

Informally a variable in C is a named object. However, there are a several trivial cases where the object doesn't have a name. 

Unless you can declare an extern variable within a function, then every variable declaration within a function is necessarily a definition

I think you just need to shut the fuck up something you clearly don't understand.

Name: Anonymous 2011-09-25 19:02

*on something*

Name: Anonymous 2011-09-25 19:18

>>17

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.

Name: Anonymous 2011-09-25 19:56

>>19

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: Anonymous 2011-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.

Name: Anonymous 2011-09-25 20:15

>>20

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.


>>21

I was talking about the type of the anonymous struct/union. Any instance of such must have a name.

Name: !!kCq+A64Losi56ze 2011-09-25 20:23

>>22
Looks leik I've been trolled. Oh well.

No you haven't you fucking illiterate jew.

What I said in >>12 was logical deduction from what was said in >>9

I, not you, wrote the response  9 and 12 you fucking retard. Oh wait, I forgot to use a tripcode. Now I'm going to.

I was talking about the type of the anonymous struct/union. Any instance of such must have a name.

Stop trolling me you fucking bitch.

Name: >>12 2011-09-25 20:36

>>23

I was >>12.

Stop trolling me you fucking bitch.

no.

Name: !!kCq+A64Losi56ze 2011-09-25 21:28

>>24

So you're the illiterate jew that wrote..

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: Anonymous 2011-09-25 21:38

it was once when the wellknown asperger known as !!kCq+A64Losi56ze wrote:

So you're the illiterate jew that wrote..

Oh, the hilarious irony!

Name: Anonymous 2011-09-25 21:58

>>25

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


c)I'm still not granting you the first interview.

:_;

d)Go scrub another toilet you mental midget.

Ok, ^_^

Name: Anonymous 2011-09-25 22:01

...just summing up stuff in this thread

malloc allocates memory, like declaring (& initializing?) a variable, except without defining a name to reference/use the memory...? hence using pointers n stuff?

Objects are variables?/ memory locations?

Unnamed objects...?
How do these not get lost?

Name: sage 2011-09-25 22:13

>>28

Just don't pay attention to the shitfags discussing retarded details no one care about in order to win an even more retarded online discussion.

Name: Anonymous 2011-09-25 22:17

=) think i'm confusing unnamed and unreferenced..

malloc(0); <<unnamed / unreferenced object with no memory allocated..?

Name: !!kCq+A64Losi56ze 2011-09-25 22:17

>>28

Objects are variables?/ memory locations?

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.

Name: !!kCq+A64Losi56ze 2011-09-25 22:18

>>29

Please tell us all what you do for a living.

Name: Anonymous 2011-09-25 22:29

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

Name: Anonymous 2011-09-25 22:34

>>31 so the object doesn't vary once its defined?

Name: sage 2011-09-25 22:34

>>33

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.

Name: Anonymous 2011-09-25 22:40

>>35
Zero-sized arrays are illegal
typedef struct {
  unsigned int size;

  char data[0];
} String;

Name: Anonymous 2011-09-25 22:45

>>35 all the old C89 standard compilers will do it?

<<34 like modifying a string creates a whole new string..?

Name: Anonymous 2011-09-25 22:45

>>33

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.

Name: Anonymous 2011-09-25 22:49

>>35

Yeah, I can't even imagine why any compiler would support variables with void type. It was ridiculous.

http://www.mixsoftware.com/product/powerc.htm

It was installed on 4 5-inch floppies I think.

Name: Anonymous 2011-09-25 22:51

>>36
there is no variable data[0] yet in that case?
still, how does that work..?

Name: sage 2011-09-25 23:03

>>36

It is illegal. Flexible arrays are illegal in C89. They're written as "[]" in C99. It does not matter whether it's supported as an extension or not. It's not pure C: it's a C dialect.

Also, it's clear from the context that I was talking about situations different from flexible arrays.

Just read the standard and fuck off.

>>37

Do what?

If the compiler goes against the standard, then it's non-conformant.
If it extends an area where the standard yields unspecified or undefined behavior, it's conformant. Programs which use the feature are conformant, but not strict-conformant.

For example, if the compiler does not yield a diagnostic message when required to, it's non-conformant. Whether it's relevant or not to the code, it does not matter: the standard does not guarantee any further behavior.

Simple as this.

>>39

I guess it would be used in situations where a type whose size is zero is useful (sizeof(void) == 0), for example, when creating some metaprogramming macros.

Name: Anonymous 2011-09-25 23:49

>>41
fuck off, Zhivago

Name: Anonymous 2011-09-26 3:01

>>23
tripcode
Well that explains everything.

>>31
So if the object has file duration
Illiterate jewish mental midget, the C standard doesn't deal with files. Again, read one of the C standards you fucking idiot.

Don't change these.
Name: Email:
Entire Thread Thread List