C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-01-2002, 09:06 PM   #1
Registered User
 
sean345's Avatar
 
Join Date: Mar 2002
Posts: 346
Register

I was wondering exactly what the register keyword does. My compiler manual, LCC-Win32, explains it as:
Quote:
Try to store the identifier in a machine register. The type of identifier will be equivalent to signed integer if not explicitly specified.
I am not exactly sure when to use it and what benifit it has.

Thanks

- Sean
__________________
If cities were built like software is built, the first woodpecker to come along would level civilization.
Black Frog Studios
sean345 is offline   Reply With Quote
Old 05-01-2002, 09:08 PM   #2
Comment your source code!
 
Lynux-Penguin's Avatar
 
Join Date: Apr 2002
Posts: 533
register keyword?
are you refering to the '=' sign?
__________________
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
Lynux-Penguin is offline   Reply With Quote
Old 05-01-2002, 09:09 PM   #3
Comment your source code!
 
Lynux-Penguin's Avatar
 
Join Date: Apr 2002
Posts: 533
perhaps this will help (it did for me)

register
The register storage class specifier indicates to the compiler that a heavily used variable (such as a loop control variable) within a block scope data definition or a parameter declaration should be allocated a register to minimize access time.

It is equivalent to the auto storage class except that the compiler places the object, if possible, into a machine register for faster access.

Note: Because the C for AIX compiler optimizes register use, it ignores the register keyword.

Most heavily-used entities are generated by the compiler itself; therefore, register variables are given no special priority for placement in machine registers. The register storage class keyword is required in a data definition and in a parameter declaration that describes an object having the register storage class. An object having the register storage class specifier must be defined within a block or declared as a parameter to a function.

The following example lines define automatic storage duration objects using the register storage class specifier:

register int score1 = 0, score2 = 0;
register unsigned char code = 'A';
register int *element = &order[0];

Initialization
You can initialize any register object except parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C expression. For structure and union members, the initial value must be a valid constant expression if an initializer list is used. The object is then set to that initial value each time the program block that contains the object's definition is entered.

Storage
Objects with the register storage class specifier have automatic storage duration. Each time a block is entered, storage for register objects defined in that block are made available. When the block is exited, the objects are no longer available for use.

If a register object is defined within a function that is recursively invoked, the memory is allocated for the variable at each invocation of the block.

The register storage class specifier indicates that the object is heavily used and indicates to the compiler that the value of the object should reside in a machine register. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers.

If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto.

Using register definitions for variables that are heavily used may make your object files smaller and make them run faster. In object code, a reference to a register can require less code and time than a reference to memory. In C programs, even if a register variable is treated as a variable with storage class auto, the address of the variable cannot be taken.

Restrictions
You cannot use the register storage class specifier in file scope data declarations.

You cannot apply the address (&) operator to register variables.
__________________
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
Lynux-Penguin is offline   Reply With Quote
Old 05-01-2002, 09:09 PM   #4
Registered User
 
sean345's Avatar
 
Join Date: Mar 2002
Posts: 346
I mean this:

Code:
register int f;
- Sean
__________________
If cities were built like software is built, the first woodpecker to come along would level civilization.
Black Frog Studios
sean345 is offline   Reply With Quote
Old 05-01-2002, 09:11 PM   #5
Registered User
 
sean345's Avatar
 
Join Date: Mar 2002
Posts: 346
Thanks

- Sean
__________________
If cities were built like software is built, the first woodpecker to come along would level civilization.
Black Frog Studios
sean345 is offline   Reply With Quote
Old 05-01-2002, 11:17 PM   #6
Unregistered
Guest
 
Posts: n/a
Quote:
Originally posted by Lynux-Penguin
It is equivalent to the auto storage class except that the compiler places the object, if possible, into a machine register for faster access.
Basically, it trys to put it directly in the CPU register for quicker math than figuring it out from the memory. My teacher explained it to me once, let's see if I can remember.

If you try and add two register variables, those variables will essentially bypass going to memory at all, and go straight to the processor's register for number crunching (if there's room). It's micro-seconds faster than typical variables because those must be pulled from memory, put into the register, then spit back into memory.
  Reply With Quote
Old 05-02-2002, 11:37 AM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
A largely useless keyword now, given that code optimisers are far better at working out what constitutes 'heavy' use (its not as obvious as it seems sometimes).

In addition, the best answer will be found each time you compile the code, not when you last sat down and thought about it.

What may seem a good choice for register on one machine, might be a bad choice on another machine.

You're also restricted as to what types will go in registers, and you can't point at a register variable (even if it actually isn't in a register).

Back in history, there was only one C compiler, and it did not have much in the way of optimisation, and this was a hint.
Salem is offline   Reply With Quote
Old 05-08-2002, 03:06 PM   #8
Registered User
 
stautze's Avatar
 
Join Date: Apr 2002
Posts: 195
It makes sence if you were programming a microchip or some thing that has limited or no cache. But all machines today have at least an L1, and most likely an L2 cache. So even if your variable isn't in the register window, it's gonna be close by in one of the cache blocks.

It sounds more dangerous that useful. It is nice to no its there but I would like to see an application of its usefulness.
stautze is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
register variables BEN10 C Programming 9 04-17-2009 07:20 AM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
register file axon Tech Board 0 11-20-2003 09:07 AM
difference between register int and normal int GanglyLamb A Brief History of Cprogramming.com 6 02-25-2003 04:01 PM
DOS, Serial, and Touch Screen jon_nc17 A Brief History of Cprogramming.com 0 01-08-2003 04:59 PM


All times are GMT -6. The time now is 09:24 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22