Jackie said:
Does anyone know when "register" declarations should be used and when
"register" must not be used? If possible please give examples for both
cases.
"register" is used when you want to hint to a compiler that a variable
should be in a hardware register for fast access.
Nowadays compilers are so good at assigning variables to registers that you
can seldom achieve any speed up by using "register", and may often slow
execution down.
register also prevents the address of a variable from being taken, so if you
need to set a pointer to that variable you may not declare it "register".
examples
void foo(void)
{
register int i;
int j;
int dummy = 0;
clock_t tick;
clock_t tock;
tick = clock();
for(i=0;i<10000;i++)
dummy *= dummy;
tock = clock();
printf("Register %d\n" (int) (tock - tick));
tick = clock();
for(j=0;i<10000;j++)
dummy *= dummy;
tock = clock();
printf("vanilla %d\n", (int) tock - tick);
}
void cursorpos(int *x, int *y)
{
/* dummy position for example */
*x = 1;
*y = 2;
}
void foo(void)
{
register int x;
register int y;
/* illegal */
cursorpos(&x, &y);
printf("x %d y %d\n", x, y);
}