Would I maybe use register in this case?

C

Chad

I'm not too sure if this is the right forum to ask the following
question.

Given the following code

#include <stdio.h>

int main(void)
{
int a=5;
int b=6;

int c, i;

for(i=0; i< 30; i++) {
c = a * b;
}

return 0;
}

Now let's assume the values of a and b never change. Each time the
'for' loop executes in the above code, a and b gets multiplied. If I
would change the value of c to register, would the values of and b get
cached? Ie not get re-loaded into the memory everytime the for loop
gets executed.

Chad
 
B

Ben Bacarisse

Chad said:
I'm not too sure if this is the right forum to ask the following
question.

Given the following code

#include <stdio.h>

int main(void)
{
int a=5;
int b=6;

int c, i;

for(i=0; i< 30; i++) {
c = a * b;
}

return 0;
}

Now let's assume the values of a and b never change. Each time the
'for' loop executes in the above code, a and b gets multiplied. If I
would change the value of c to register, would the values of and b get
cached? Ie not get re-loaded into the memory everytime the for loop
gets executed.

You are begging the question. A good compiler will generally do
better register allocation if you stay out of its hair (hence the
register keyword is sometimes ignored by compilers and rarely used by
programmers).

Regardless of the storage class of c, a good compiler will do much
more to above than 'cache' a and b -- it will remove the loop entirely
and may well remove the whole program since it can prove that setting
c makes no difference to anyone!
 
C

Chad

You are begging the question. A good compiler will generally do
better register allocation if you stay out of its hair (hence the
register keyword is sometimes ignored by compilers and rarely used by
programmers).

Regardless of the storage class of c, a good compiler will do much
more to above than 'cache' a and b -- it will remove the loop entirely
and may well remove the whole program since it can prove that setting
c makes no difference to anyone!

Thank you for taking the time to clarify my apparent brain fart.
 
B

Barry Schwarz

I'm not too sure if this is the right forum to ask the following
question.

Given the following code

#include <stdio.h>

int main(void)
{
int a=5;
int b=6;

int c, i;

for(i=0; i< 30; i++) {
c = a * b;
}

return 0;
}

Now let's assume the values of a and b never change. Each time the
'for' loop executes in the above code, a and b gets multiplied. If I
would change the value of c to register, would the values of and b get
cached? Ie not get re-loaded into the memory everytime the for loop
gets executed.

The C language doesn't know from cache. Whether a value is cached is
a question usually beyond the compiler/linker/run time library, the
things we usually discuss here.

Does the hardware have cache? How much? Does the operating system
support it efficiently? What else is running on the system at the
time this code is running? Is the code generated by the compiler
"cache-friendly"?

None of these questions are affected by presence or absence of
"register". It is entirely possible that adding register would cause
data that would normally be cached during execution to not be.

By the way, most modern compilers with a decent optimizer will ignore
register when you specify it.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top