Register variable... where are you?

This is a discussion on Register variable... where are you? within the C Programming forums, part of the General Programming Boards category; Hi , I'm Fabio from Italy. Is it possible to know if a variable declared as register is really allocated ...

  1. #1
    Registered User pollopinolo's Avatar
    Join Date
    Dec 2005
    Location
    Milan, Italy
    Posts
    3

    Register variable... where are you?

    Hi ,
    I'm Fabio from Italy.

    Is it possible to know if a variable declared as register is really allocated in the register?


    Thanx a lot!
    Fabio

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Most compilers have the option of outputting assembly code. Typically, you should not use the register keyword and just let the compiler do its thing.

    Following are examples of how to output assembly using GCC and MSVC respectively (with O2 optimization):
    Code:
    gcc.exe -S -O2 test.c
    cl /FAs /O2 test.c

  3. #3
    Registered User pollopinolo's Avatar
    Join Date
    Dec 2005
    Location
    Milan, Italy
    Posts
    3
    Typically, you should not use the register keyword and just let the compiler do its thing.
    In order to minimize the computation time, I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles,
    If I write a declaration as follow:

    Code:
    register int i
    ... I hope that "i" variable will be allocated in the register, but, if the register is full, "i" will be allocated in RAM.
    The compiler makes always the right choice , but are you sure that this choice is demanded to the compiler and not to the operative system?

    In other words I think (but I'm not really sure!) that the allocation in the register is a run-time choiche of the O.S. and it depends on the state of the register.
    If the register is full the "i" variable is allocated in RAM, but, if the register is free, the "i" variable is allocated in the register.

    What do you think about it?


    I've just tried to explain this concept in the best way, but I don't speak English very well... excuse me !

    Fabio

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles
    How many is a lot? Do you have enough registers and are they all devoted to your program? Does your compiler even honor the hint?

    >What do you think about it?
    If a variable is specified as being register, the compiler is free to take that as a hint that it's going to be used heavily and optimize accordingly. But a lot of compilers completely ignore the register keyword because the compiler is in a much better position to know how and where to optimize your code than you are. The common guideline these days is to avoid the register keyword and let your compiler do its job. If the variable is really a hog then it'll probably be placed in a register anyway.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Most modern compilers will optimise code far better then the average programmer. Trying to force your own optimisation onto code will rarely have a positive effect, and often a negative one. Compilers allow the use of the "register" storage modifier to be compliant with standards, but most will simply ignore it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User pollopinolo's Avatar
    Join Date
    Dec 2005
    Location
    Milan, Italy
    Posts
    3
    How many is a lot? Do you have enough registers and are they all devoted to your program?
    I wrongly said "I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles".

    This is my corrected sentence:"I want to force the allocation in the register of an index variable used in a for-statement (10000000 cycles)"

    I need a single index variable and I would test the time difference between register variable and RAM variables... but I think I'll follow your subsequent suggestions!
    1)
    Trying to force your own optimisation onto code will rarely have a positive effect, and often a negative one.
    2)
    The common guideline these days is to avoid the register keyword and let your compiler do its job. If the variable is really a hog then it'll probably be placed in a register anyway.
    Thanks to all for your support!

    Bye Fabio
    Last edited by pollopinolo; 12-28-2005 at 07:33 AM. Reason: missing

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by pollopinolo
    In other words I think (but I'm not really sure!) that the allocation in the register is a run-time choiche of the O.S. and it depends on the state of the register.
    If the register is full the "i" variable is allocated in RAM, but, if the register is free, the "i" variable is allocated in the register.
    No, register allocation (at least on all of the architectures that I've worked on) is done by the compiler at final code generation time. (Actually, I know of at least one system where this was done at image link time, but the linker in that case was doing major code optimization, so I consider it a deferred stage of the code generation from the compiler.)

    The register keyword is a hint to the compiler that, when deciding between candidates for binding to registers with otherwise equal weight, the programmer thinks this is a better candidate. With modern code generators this hint is of little value, since the optimizations within the compiler are far more sophisticated than they were back in the 1970s, when the C language was put together.

    [edit]The original K&R C compiler was written for the PDP-11 which had 8 general purpose registers (only 6 of these could be used arbitrarily); these could be used to hold pointers, indexes, or data. The optimizer in this compiler was very primitive (if it existed at all). In those days, the register qualifier was almost always honored unless there were too many variables declared as register or the type was incompatible with being a register. Taking the address of a register variable generated an error message from some compilers, other compilers simply forgot the qualifier in these cases.[/edit]
    Last edited by filker0; 12-28-2005 at 08:07 AM. Reason: Add a short historical point and fix grammer
    Insert obnoxious but pithy remark here

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In other words I think (but I'm not really sure!) that the allocation in the register is a run-time choiche of the O.S. and it depends on the state of the register.
    Could you be thinking of the CPU cache (also wikipedia) rather than registers?
    "I want to force the allocation in the register of an index variable used in a for-statement (10000000 cycles)"
    A loop variable will almost certainly be placed in a register. You can confirm this by looking at the assembly output of programs.

    If your code is running too slow, putting a variable into a register is not going to fix it. To get real speed improvements, you need to start by reducing calls to expensive functions (for example, output to the console is extremely expensive) and most importantly reducing the number of loop iterations.

    Using time testing is a always a good idea rather than just using what "should" work.

    filker0, thanks for another informative post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Register variable
    By Lettin03 in forum C Programming
    Replies: 2
    Last Post: 11-20-2007, 06:14 AM
  2. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 09:46 AM
  4. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 08:32 AM
  5. Replies: 2
    Last Post: 04-12-2004, 02:37 AM

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