In order to establish whether the register keyword has any significanse, tiny example codes won't do. Here is a c-code which suggests to me, the register keyword still has a significance. But it might be different with GCC on Linux, I don't know.
WILL register int k & l be stored in a CPU register or not ? Linux users (especially) should compile with GCC and optimization. With Borland bcc32 the register keyword appears to function (in this example), as the &-operator gives error codes for register declared integers. NOTE! This is NOT the case with a tiny example with Borland on Windows ! In order to really see what the compiler optimizes or not, it has to be a more than a tiny example. Empty loops won't do ! Nevertheless - IF an address CAN be read with the &-operator, the variable isn't stored in a CPU register. But if a register declared variable can't be read (causing error code at compilation) - I have to presume that the register keyword actually do put the variable in a CPU-register. It might differ on various platforms, I don't know. (If it works , the number of "ticks" will be far lower with the register declaration.
/* reg_or_not.c */
#include <stdio.h>
#include <time.h>
#include <stdlib> //not requiered for Linux
#define LAPSb 50
#define LAPS 50000
#define MAXb 50
#define MAX 50000
int main (void)
{
/* 20 ints and 2 register ints */
register int k,l;
int a,aa,b,bb,c,cc,d,dd,e,ee,f,ff,g,gg,h,hh,i,ii,j,jj;
/* measure some ticks also */
clock_t start_1,start_2;
clock_t finish_1,finish_2;
long tmp; //just for the workload
/* pointer declarations of all ints */
int *ap, *aap, *bp, *bbp, *cp, *ccp, *dp, *ddp, *ep, *eep;
int *fp, *ffp, *gp, *ggp, *hp, *hhp, *ip, *iip, *jp, *jjp;
int *kp,*lp;
/* end of declarations */
/* read memory addresses, if possible - which can't be done in a CPU-register */
ap=&a; aap=&aa; bp=&b; bbp=&bb;
cp=&c; ccp=&cc; dp=&d; ddp=ⅆ
ep=&e; eep=ⅇ fp=&f; ffp=&ff;
gp=&g; ggp=≫ hp=&h; hhp=&hh;
ip=&i; iip=ⅈ jp=&j; jjp=&jj;
//kp=&k; //won't compile if k is stored in a CPU register
//lp=&l; //same - but try both ways !
/* what address , isn't the issue in this case - but if stored in memory some "crazy" number will be shown, whilst CPU-registers can't be read */
printf("Address a aa: %u %u\n",a,aa);
printf("Address b bb: %u %u\n",b,bb);
printf("Address c cc: %u %u\n",c,cc);
printf("Address d dd: %u %u\n",d,dd);
printf("Address e ee: %u %u\n",e,ee);
printf("Address f ff: %u %u\n",f,ff);
printf("Address g gg: %u %u\n",g,gg);
printf("Address h hh: %u %u\n",h,hh);
printf("Address i ii: %u %u\n",i,ii);
printf("Address j jj: %u %u\n\n",j,jj);
//printf("Address k: %u \n",k); //no reason to try "k" actually is in a CPU-register
//printf("Address l: %u \n",l);
start_2=clock(); //just for fun
/* to ensure workload */
for (a=1;a<LAPSb;a++) {for (aa=0;aa<MAXb;aa++);{tmp+=aa/a;}}
for (b=1;b<LAPSb;b++) {for (bb=0;bb<MAXb;bb++);{tmp+=aa/a;}}
for (a=1;c<LAPSb;c++) {for (cc=0;cc<MAXb;cc++);{tmp+=bb/b;}}
for (d=1;d<LAPSb;d++) {for (dd=0;dd<MAXb;dd++);{tmp+=cc/c;}}
for (e=1;e<LAPSb;e++) {for (ee=0;ee<MAXb;ee++);{tmp+=dd/d;}}
for (f=1;f<LAPSb;f++) {for (ff=0;ff<MAXb;ff++);{tmp+=ee/e;}}
for (g=1;g<LAPSb;g++) {for (gg=0;gg<MAXb;gg++);{tmp+=ff/f;}}
for (h=1;h<LAPSb;h++) {for (hh=0;hh<MAXb;hh++);{tmp+=hh/h;}}
for (jj=1;jj<LAPSb;jj++) {for (ii=0;ii<MAXb;ii++);{tmp+=ii/jj;}}
start_1=clock(); //see following printf
for (i=0;i<LAPS;i++) {for (j=0;j<MAX;j++);{tmp+=j/i;}} /* same double loop - in supposed memory */
finish_1=clock(); //see following printf
printf ("Memory: %ld ticks\n\n", finish_1 - start_1); //ticks for memory
start_1=clock(); //see following printf
for (k=0;k<LAPS;k++) {for (l=0;l<MAX;l++);{tmp+=l/k;}} /* same double loop - in supposed register*/
finish_1=clock(); //see following printf
printf ("Register: %ld ticks\n\n", finish_1 - start_1); //ticks for CPU register (?) any difference ?
finish_2=clock();
printf ("Total: %ld ticks\n\n", finish_2 - start_2); //really for fun only
system("PAUSE"); //only requiered for Windows, so the CMD-window doesn't vanish
return 0;
}
constkeyword but this question proved that I was wrong. So I'll wait and see what I get. – Aseem Bansal Jul 29 '13 at 11:22constkeyword is something different against register. – Uwe Plonus Jul 29 '13 at 11:45registercan be quite useful in certain limited contexts. I've used it a handful of times to fix mal-optimization for a bit of embedded firmware. The biggest issue with using it is that you have to make sure your compiler doesn't change out from under you so that it becomes you who is slowing the execution down. – jkerian Jul 30 '14 at 08:47