register keyword

Discussion in 'C Programming' started by Bill Cunningham, Oct 23, 2013.

  1. I was talking with some old time programmers about C and register. It
    was used to do things quicker at least at one time. Adding 2 ints for
    example was done in a register instead of memory.

    Is register still used or does it even work anymore? I know it's around
    for prosterity but does it do anything anymore? Like actually add values in
    a processor register?

    Bill
     
    Bill Cunningham, Oct 23, 2013
    #1
    1. Advertisements

  2. Most compilers now are better at deciding what, and when, something
    should be in a register.
    It might be that some still do keep some variables in a (small)
    set of registers, but it also tells the compiler that you won't
    have any pointers to that variable, which can help optimization.

    -- glen
     
    glen herrmannsfeldt, Oct 23, 2013
    #2
    1. Advertisements

  3. It's hardly ever used, but I posted an example the other day. It was a function
    that rebooted the computer. It made calls to reset the bss and stack, which
    can't be written in any kind of portable C, then called main. If it called
    main directly the optimiser might try cross-function optimisation, which you
    don't want because you've trashed the stack. So you can usually get rid of
    this by calling indirectly. But you've trashed the stack, so a local variable
    might be corrupted. If you hold it in a register, you've still got it.

    It's hacky and it won't always work. C's often like that.
     
    Malcolm McLean, Oct 23, 2013
    #3
  4. The common wisdom is that the "register" keyword is not currently
    useful, because optimizing compilers are typically better
    than programmers at deciding which variables should be stored
    in registers. I'm not sure that's *always* the case, but it's
    pretty close.

    The standard's definition of the "register" keyword doesn't actually
    say anything about CPU registers. N1570 6.7.1p6:

    A declaration of an identifier for an object with storage-class
    specifier register suggests that access to the object be as
    fast as possible. The extent to which such suggestions are
    effective is implementation-defined.

    Elsewhere, the standard forbids taking the address of a
    register-qualified object.

    It doesn't necessarily have anything to do with "actually add[ing]
    values in a processor register". On many CPUs, arithmetic operations
    can only be applied to registers anyway (and variables in memory
    have to be loaded into a register before operating on them).
     
    Keith Thompson, Oct 23, 2013
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.