473,587 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8595
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**********@e arthlink.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("Registe r %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.u k> wrote in message
news:ce******** **@newsg3.svr.p ol.co.uk...

"Jackie" <Ja**********@e arthlink.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("Registe r %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**********@e arthlink.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:um******** *************** *********@4ax.c om...
On Tue, 27 Jul 2004 18:02:49 GMT, "Jackie"
<Ja**********@e arthlink.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*******@spam cop.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*******@spam cop.net> wrote:
On Tue, 27 Jul 2004 18:02:49 GMT, "Jackie"
<Ja**********@e arthlink.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(unsig ned 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(unsig ned 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_LO OP 4

for (i = 0;
i + ACCESSES_PER_LO OP < quantity;
i += ACCESSES_PER_LO OP)
{
/* 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.l earn.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
3253
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 full 10 minutes every time a "register" declared variable is read from or written to. Besides this lag, everything else runs as expected. Then my compiler is still C compliant, aye? If so, then it is unwise for any programmer to ever use the...
0
7924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.