Thread: register variables

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    register variables

    hi,

    when using the register keyword, the variable is stored in the cpu.....

    does anyone know how many register variables you can have in a function? assuming x86 architecture...

    Thanks

    null

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    I think ax, bx, cx and dx are the general purpose ones (eax, ebx, ecx and edx in 32 bits) so thats 4 registers... Usually the compiler will make some variables register vars automagically when it's set to optimizing (ie VC6++), and sometimes setting register vars explicity will choke the optimizer...

    Then again, it can make loops very fast when used properly.. The compiler will make as many of the register vars you specify true register vars, and when it runs out of free registers it will just ignore the other requests, I think. Sometimes the compiler might require a spare register, say, for optimizing math, etc.. so you might not always have 4 or even 3.
    .sect signature

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm... if I might say something about register variables...

    Your compiler doesn't have to actually use register variables. According to the ANSI standard, whether or not they are implemented in any fashion is up to the implementor.

    Still, it's worth experimenting with, although it's becoming a rupee of the language.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > when using the register keyword, the variable is stored in the cpu
    In the days before optimisers, this was used as a 'hint' (not obligation) to suggest that the compiler place the value in a register.

    Optimising compilers have a much better idea of which variables are used most often (and hence might be better in registers), and will do this every time you compile the code. Without a real good knowledge of how your compiler generates code, it's at best guesswork.

    > Your compiler doesn't have to actually use register variables.
    Except you still can't point at a register variable (as far as I know).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    you are right , irrespective of whether the variable is actually put in a register , one cannot point to the variable

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    thanks for the help guys...and sorry for late reply.


    null

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Just a "hint":

    The best variables to put in the register are variables that are used a lot (example, loop variables).

    Just thought that I would share that little tid-bit of information with you.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM