473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

register keyword

Hi everyone,
Does anyone know when "register" declarations should be used and when
"register" must not be used? If possible please give examples for both
cases.
Thanks
Nov 14 '05 #1
9 8581
Jackie wrote on 27/07/04 :
Hi everyone,
Does anyone know when "register" declarations should be used and when
"register" must not be used? If possible please give examples for both
cases.


Don't use 'register'. The compiler makes it for you.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

"Jackie" <Ja**********@earthlink.net> wrote

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);
}
Nov 14 '05 #3
Hi Malcom,
I tried the code right below this message. It doesn't prevent the variable
"i" address from being taken. I could compile and run it under gcc. (It
compiles with this warning "address of register variable `i' requested" but
nothing is prevented)
int main()
{
register int i = 0;
int j = 0;
i++;

int * p = &i;
int * q = &j;

*p = 10;
*q = 8;

printf("i = %d\n", i);
printf("j = %d\n", j);
}
OUTPUT:
i = 10
j = 8
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:ce**********@newsg3.svr.pol.co.uk...

"Jackie" <Ja**********@earthlink.net> wrote

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);
}

Nov 14 '05 #4

On Tue, 27 Jul 2004, Jackie wrote:

I tried the code right below this message. It doesn't prevent the variable
"i" address from being taken. I could compile and run it under gcc. (It
compiles with this warning "address of register variable `i' requested" but
nothing is prevented) <snip> register int i = 0;
int * p = &i;


Because it's annoying as hell.
Why not?
Please don't top-post.

As to your question: Your compiler is certainly allowed to produce
whatever executable program it likes, for whatever reason it likes.
It has kindly warned you that your source code is /not/ valid C,
and (in this case) it has gone ahead and produced an executable
anyway. That doesn't mean you ought to ignore the diagnostic.

The code as written /is/ incorrect; you /cannot/ take the address
of an object declared 'register'.

<OT> Try 'gcc -W -Wall -ansi -pedantic -O2'; this is the command
line I find most useful for diagnosing invalid or potentially
ambiguous C code. ('-W -Wall' is considered overkill by some
regulars here.) </OT>

HTH,
-Arthur
Nov 14 '05 #5
On Tue, 27 Jul 2004 18:02:49 GMT, "Jackie"
<Ja**********@earthlink.net> wrote in comp.lang.c:
As Malcom said, don't top post.
Hi Malcom,
I tried the code right below this message. It doesn't prevent the variable
"i" address from being taken. I could compile and run it under gcc. (It
compiles with this warning "address of register variable `i' requested" but
nothing is prevented)
int main()
{
register int i = 0;
int j = 0;
i++;

int * p = &i;
int * q = &j;

*p = 10;
*q = 8;

printf("i = %d\n", i);
printf("j = %d\n", j);
}
OUTPUT:
i = 10
j = 8


Then whatever compiled this was not a standard C compiler. It might
be a C compiler that was not invoked in standard C mode, and so it
allowed some non-standard compiler-specific extensions. Or it might
be that you compiled this with a C++ compiler, where taking the
address of a register variable happens to be legal.

But whatever the reason, you did not compile this with a standard C
compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:um********************************@4ax.com...
On Tue, 27 Jul 2004 18:02:49 GMT, "Jackie"
<Ja**********@earthlink.net> wrote in comp.lang.c:

As Malcom said, don't top post.
[Actually, it was Arthur.]
I tried the code right below this message. It doesn't prevent the variable "i" address from being taken. I could compile and run it under gcc. (It
compiles with this warning "address of register variable `i' requested" but nothing is prevented)
int main()
{
register int i = 0;
int j = 0;
i++;

int * p = &i;
int * q = &j;

*p = 10;
*q = 8;

printf("i = %d\n", i);
printf("j = %d\n", j);
}
OUTPUT:
i = 10
j = 8


Then whatever compiled this was not a standard C compiler. It might
be a C compiler that was not invoked in standard C mode, and so it
allowed some non-standard compiler-specific extensions.


% gcc -W -Wall -ansi -pedantic -ffloat-store jackie.c
jackie.c: In function `main':
jackie.c:7: warning: address of register variable `i' requested
jackie.c:7: warning: ISO C89 forbids mixed declarations and code
jackie.c:13: warning: implicit declaration of function `printf'
jackie.c:15: warning: control reaches end of non-void function

% a.exe
i = 10
j = 8

%

In what way did I not invoke gcc as a conforming compiler?
Or it might
be that you compiled this with a C++ compiler, where taking the
address of a register variable happens to be legal.
[But not supplying a prototype for printf isn't.]
But whatever the reason, you did not compile this with a standard C
compiler.


I think you've jumped the gun.

--
Peter
Nov 14 '05 #7

"Jack Klein" <ja*******@spamcop.net> wrote in message

Or it might be that you compiled this with a C++ compiler, where taking the address of a register variable happens to be legal.

That's a little factoid I was unaware of.
Nov 14 '05 #8
Jack Klein <ja*******@spamcop.net> wrote:
On Tue, 27 Jul 2004 18:02:49 GMT, "Jackie"
<Ja**********@earthlink.net> wrote in comp.lang.c:
I tried the code right below this message. It doesn't prevent the variable
"i" address from being taken. I could compile and run it under gcc. (It
compiles with this warning "address of register variable `i' requested" but
nothing is prevented)
Then whatever compiled this was not a standard C compiler. It might
be a C compiler that was not invoked in standard C mode, and so it
allowed some non-standard compiler-specific extensions.


Why? It's a constraint violation. It requires a diagnostic. A diagnostic
was provided. Sounds conforming to me.

Richard
Nov 14 '05 #9
Jackie wrote:
Hi everyone,
Does anyone know when "register" declarations should be used and when
"register" must not be used? If possible please give examples for both
cases.
Thanks


The register keyword should be used as a last resort when
optimizing a function; and it is platform dependent as to
whether it will be efficient or not. By the way, the
"register" keyword is only a hint to the compiler. The
compiler is allow to ignore your suggestion.

Given a program fragment to sum array locations:

#define ARRAY_SIZE 102400

unsigned int array[ARRAY_SIZE];

// Assume array has been filled with valid integers.
unsigned int Array_Sum(unsigned int * p_array,
unsigned int quantity)
{
unsigned int i;
unsigned int sum = 0;

for (i = 0; i < quantity; ++i)
{
sum += p_array[i];
}
return sum;
}

In the above function, Array_Sum, the array is
accessed one location at a time. In generic
computer theory, accessing many memory locations
at once is faster than many accesses to many
memory locations.

Here is the same function, but applying the
above idea and loop unrolling:
unsigned int Array_Sum(unsigned int * p_array,
unsigned int quantity)
{
unsigned int i;
unsigned int sum = 0;

/* declare some temporary values to use the
processors "extra" registers */
register unsigned int r1, r2, r3, r4;
#define ACCESSES_PER_LOOP 4

for (i = 0;
i + ACCESSES_PER_LOOP < quantity;
i += ACCESSES_PER_LOOP)
{
/* Read from memory into several registers */
r1 = p_array[i + 0];
r2 = p_array[i + 1];
r3 = p_array[i + 2];
r4 = p_array[i + 3];

/* Calculate sum using registers */
sum += r1;
sum += r2;
sum += r3;
sum += r4;
}

/* Sum up remaining numbers */
for (; i < quantity; ++i)
{
sum += p_array[i];
}
return sum;
}

Hopefully, the compiler is smart enough to recognize the
reading pattern and use specialized processor instructions.
The loop unrolling serves two purposes: 1) More data
processing instructions are exected per branch instructions;
and 2) Allows for multiple fetches from memory at one time.
Whether or not the compiler or process takes advantage of
2), depends on the compiler and the platform. This solution
works nice on an ARM7 or ARM9 processor that has a
special instruction for loading multiple registers from
memory with one instruction.

So the "register" keyword along with loop unrolling
help form an optimization pattern. A good compiler will
take the hints and generate appropriate code. If the
compiler doesn't generate the optimized code, there is
always the option of writing the code in assembly language.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.