register variables

This is a discussion on register variables within the C Programming forums, part of the General Programming Boards category; Halo all, This is a line from K&R(section 4.7) regarding register variables. A register declaration advises the compiler that the ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    register variables

    Halo all,
    This is a line from K&R(section 4.7) regarding register variables.
    A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be stored in machine registers,which may result in smaller and faster programs.but compilers are free to ignore the advice.
    Now my doubt is :

    If by declaring any variable register, that variable gets stored in machine registers, where are other variables(auto,static,extern) get stored. i mean if i do
    Code:
    int a;
    this reserves space for 'a' in memory somewhere but memory is also a group of registers, so in effect it also gets stored in registers?
    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In your example, a may be in a register or it may be in memory. It may be in both at some point in time (because the way the compiler decides to solve the coding of the particular bit of code requires it uses a register for a) - it is entirely up to the compiler.

    The register keyword is pretty useless these days, because compilers can figure out (often much better than humans) which variables should go in which register, and which should be held in memory. If the compiler pays any attention at all to the register keyword, it may actually end up being WORSE than the code the compiler would come up with - particularly on "low register" machines such as x86-32. There are, after all, only at most 7 usable registers in x86, and most of the time, there's actually only 6 registers, as EBP is used a s frame-pointer. Three registers are "scratch" registers, so they (may) loose their value when calling another function, which leaves three, or at most 4, registers that the compiler can "play" with - if you then use up one of those by using the register keyword, there is only 2 or 3 left for the compiler to use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by BEN10
    memory is also a group of registers
    No, registers are within the CPU and are much faster for the processor to access than main memory.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    No, registers are within the CPU and are much faster for the processor to access than main memory.
    what do mean by main meomory.is it RAM?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    what do mean by main meomory.is it RAM?
    Yes, memory in this case is RAM (or possibly ROM, but a variable that you want to change would obviously not be possible in ROM, as you can't write [by usual processor "store" operations at least] into ROM).

    And a register is of course a form of memory, but it is internal to the CPU itself, and it is a direct part of the processor execution unit or Arithmetic Logic Unit as it is commonly called (ALU).

    The RAM will also, most likely, be copied into a L2 or L1 cache in the processor, which again is memory, but it is part of the CPU itself - but NOT part of the ALU.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    Yes, memory in this case is RAM (or possibly ROM, but a variable that you want to change would obviously not be possible in ROM, as you can't write [by usual processor "store" operations at least] into ROM).

    And a register is of course a form of memory, but it is internal to the CPU itself, and it is a direct part of the processor execution unit or Arithmetic Logic Unit as it is commonly called (ALU).

    The RAM will also, most likely, be copied into a L2 or L1 cache in the processor, which again is memory, but it is part of the CPU itself - but NOT part of the ALU.

    --
    Mats
    it means the register varaibles are stored in special registers like accumulator(which is a part of ALU) etc. and others(auto) are stored at some place in RAM(which is NOT a part of ALU).
    also as u said 'a' can be stored in ALU or RAM depending on the compilers choice.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    it means the register varaibles are stored in special registers like accumulator(which is a part of ALU) etc. and others(auto) are stored at some place in RAM(which is NOT a part of ALU).
    also as u said 'a' can be stored in ALU or RAM depending on the compilers choice.
    Yes. Modern processors tend not to have dedicated accumulator registers and such, but rather have a set of registers that are essentially all the same. But as a principle all that you said above is correct. And for certain, the compiler may choose for any number of reasons to put a register in either memory or register, no matter whether you use the register keyword or not [it MAY, at very high warning levels, inform that you are using register when it can't put the variable in a register, but I think most compilers don't].

    I would certainly recommend to NOT use the register keyword EVER in new code. It doesn't help.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    Yes. Modern processors tend not to have dedicated accumulator registers and such, but rather have a set of registers that are essentially all the same. But as a principle all that you said above is correct. And for certain, the compiler may choose for any number of reasons to put a register in either memory or register, no matter whether you use the register keyword or not [it MAY, at very high warning levels, inform that you are using register when it can't put the variable in a register, but I think most compilers don't].

    I would certainly recommend to NOT use the register keyword EVER in new code. It doesn't help.

    --
    Mats
    Thanks mats.i just wanna know as u said in ur previous post
    Yes, memory in this case is RAM (or possibly ROM, but a variable that you want to change would obviously not be possible in ROM, as you can't write [by usual processor "store" operations at least] into ROM).
    are variables ever stored in ROM coz they can't be altered there.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    Thanks mats.i just wanna know as u said in ur previous post

    are variables ever stored in ROM coz they can't be altered there.
    Something like:
    Code:
    const int arr[100] = { ... }
    may well be stored in ROM on an embedded system.

    Obviously, as I stated, there is no way to (trivially) alter things in a ROM, so normal C code could not possibly use ROM as writable memory. For certain types of ROM (e.g. FLASH memory), it can be written using "programming sequences", but that is really a different matter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    Something like:
    Code:
    const int arr[100] = { ... }
    may well be stored in ROM on an embedded system.

    Obviously, as I stated, there is no way to (trivially) alter things in a ROM, so normal C code could not possibly use ROM as writable memory. For certain types of ROM (e.g. FLASH memory), it can be written using "programming sequences", but that is really a different matter.

    --
    Mats
    Thanks mats once again.u cleared my concepts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 06:34 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM
  4. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM
  5. register variables
    By null in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 05:47 PM

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