Teen Programmers Unite  
 

 

Return to forum top

register keyword in C

Posted by DragonWolf [send private reply] at July 26, 2002, 05:22:29 AM

How can I unallocate a register variable?

Posted by Psion [send private reply] at July 26, 2002, 08:00:43 AM

The register keyword in C is obsolete. Compilers now decide what should be in registers better than you can. However, there was _never_ any concept of "allocating" or "unallocating register variables." Your best bet would probably be to remove all uses of "register" and go on with your life. =)

Posted by DragonWolf [send private reply] at July 26, 2002, 08:14:43 AM

"And life goes on..... EXCEPT FOR YOU! HAHA!" -- Bender, Futurama

Thanx

Posted by unknown_lamer [send private reply] at July 26, 2002, 11:21:44 AM

No, register is still used as a hint to the compiler. At least for GCC 3.1 (the manual says so). It might not listen to you, but it can help it (e.g. like declare does in common-lisp code).

Posted by Mike_L [send private reply] at July 26, 2002, 11:34:27 AM

Variables in C are unallocated when they go out of scope. Look at this code snippet:

void foo() {
int bar;
blah( bar );
}

When the program flow enters foo(), the variable bar is allocated on the heap. bar remains on the heap while the program flow enters blah() and returns. When the program flow reaches the closing brace, }, the variable bar goes out of scope and is unallocated from the heap.

You should forget about the register keyword and go read up on C scoping.

Posted by Zandalf [send private reply] at July 26, 2002, 06:40:38 PM

Mike - wait a sec... how does that have _anything_ to do with the register keyword? register doesn't deal with scope, it deals with access speed. or rather, hypothetical access speed.

Posted by unknown_lamer [send private reply] at July 26, 2002, 07:29:23 PM

Well, DragonWolf wanted to know how to unallocate a register variable. The answer is that it is automatically unallocated when it goes out of scope. So if the compiler decided to listen to you, you'll be short a register for the entire scope of the variable. Of course, the loss of that register is made up if you access the register variable a lot. I wish the x86 had more registers (8 registers! WTF! The x86-64 is only going to have 16 too...jeez, get more registers already!)

Posted by Neumann [send private reply] at July 26, 2002, 08:52:57 PM

The Sun SPARC I'm "learning" assembly on has 520 registers available through registers Windows... now thats ALOT of registers.

Posted by DragonWolf [send private reply] at July 29, 2002, 03:21:29 AM

The reason I wanted to know is that I might us a register if I'm doing alot of math on a variable and don't need the other registers. But I want ot change the type of the register half way through. (obviously without casting)

register char myChar
do some stuff
finish with char
regsiter short myShort
do some more stuff

If I don't unallocate the register than the char register is being used up the whole time.
(I think you need to declare register variables at the begining of scope as well)

This could be solved if I can manually change the type of the register variable

Posted by DragonWolf [send private reply] at July 29, 2002, 03:37:28 AM

I know how scope works I've been programming for more than 10 years ^^ Just never had a need to use the register variable till now ^^

Posted by buzgub [send private reply] at July 29, 2002, 04:04:04 AM

This seems like a stupid idea to me. If you really want to do it, though, here's a tip or two.

In C, you can put a block (as indicated by { ... } ) anywhere.

You can also declare any variables at the start of any blocks. They will fall out of scope at the end of the block. It is therefore legal to do this:


#include <stdio.h>

#define DEMONSTRATE_BROKEN 1
#undef DEMONSTRATE_BROKEN

int main (void){
        {
                int daft_variable = 3;
                printf("daft_variable is %d\n", daft_variable);
        }
#ifdef DEMONSTRATE_BROKEN
        printf("daft_variable is %d\n", daft_variable);
#endif
        {
                char daft_variable = 2;
                printf("daft_variable is %d\n", daft_variable);
        }
}



bear in mind that if you try to do anything with daft_variable between those two blocks, it won't work. Our daft friend will be out of scope again. Delete that "#undef DEMONSTRATE_BROKEN" to see that happen.
Posted by DragonWolf [send private reply] at July 30, 2002, 08:25:58 AM

thx

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.