register keyword

B

Bill Cunningham

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
 
G

glen herrmannsfeldt

Bill Cunningham said:
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.

Most compilers now are better at deciding what, and when, something
should be in a register.
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?

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
 
M

Malcolm McLean

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?
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.
 
K

Keith Thompson

Bill Cunningham said:
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?

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).
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top