Register

This is a discussion on Register within the C Programming forums, part of the General Programming Boards category; I was wondering exactly what the register keyword does. My compiler manual, LCC-Win32, explains it as: Try to store the ...

  1. #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:
    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

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

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

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

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

  6. #6
    Unregistered
    Guest
    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.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    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.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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