0

Possible Duplicate:
“register” keyword in C?

What if I declare almost everything as register variables? This will speed up my program yes? Or is there some sort of drawback. Cause I'm having a hard time picking with ones to declare as register variables.

Community
  • 1
  • 1
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
  • The compiler generally optimizes better than you, don't spend time on this. – DCoder Nov 24 '12 at 14:16
  • See also: http://stackoverflow.com/questions/3500301/can-gcc-g-tell-me-when-it-ignores-my-register – Mat Nov 24 '12 at 14:20

1 Answers1

2

You can't take addresses of register variables.

Apart from that, compilers are good at register allocation, and you pretty much never want to declare variables as register yourself (compilers are free to ignore the hint -- I would not be surprised if major compilers pretend that register does nothing, except perhaps when told not to optimize the code).

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • but I've read somewhere that it will make my programs faster? Is it outdated now? – jantristanmilan Nov 24 '12 at 14:16
  • @vincentbelkin: it does not. Register allocation is something the compiler does very well nowadays (read: better than you would do). – Alexandre C. Nov 24 '12 at 14:18
  • 2
    @vincentbelkin: Yes, that information is outdated. In some cases, the `register` keyword may even make the program *slower* (if the compiler honors the keyword and the register allocation turns out to be sub-optimal) – Bart van Ingen Schenau Nov 24 '12 at 14:34
  • @BartvanIngenSchenau Thanks! Cause old books on C keep telling its for optimization. Glad I chose to clear up things, or I might have kept doing it. – jantristanmilan Nov 24 '12 at 14:36
  • @vincent - You shouldn't read books that old! On a PC, careful use of `register` could improve code before about 1985. After that, the compilers will do it better than humans. – Bo Persson Nov 24 '12 at 16:11