473,396 Members | 1,918 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.

Would I maybe use register in this case?

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

Sep 3 '07 #1
3 1154
Chad <cd*****@gmail.comwrites:
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!

--
Ben.
Sep 3 '07 #2
On Sep 2, 5:34 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Chad <cdal...@gmail.comwrites:
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!

--
Ben.
Thank you for taking the time to clarify my apparent brain fart.
Sep 3 '07 #3
On Sun, 02 Sep 2007 17:01:11 -0700, Chad <cd*****@gmail.comwrote:
>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
Sep 3 '07 #4

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

Similar topics

20
by: Thomas Heller | last post by:
I'm currently working on a new version of py2exe, which will require Python 2.3 and later, because it uses the zipimport mechanism. Since py2exe is a distutils extension, and since C compilers...
16
by: alakesh.haloi | last post by:
Hi All Is there a limit on the number of register variables that can be declared and initialized in a C program on a particular hardware platform? If you define more number of register variables...
16
by: junky_fellow | last post by:
what is the purpose of declaring a register variable ? why can't we find the address of register variable ?
5
by: richard.tallent | last post by:
I'm fond of web controls in ASP.NET, but the need for <@ Register%> blocks at the top of each page is a pain. ASP.NET 2.0 has a way to centrally register controls in the web.config file, but it...
4
by: DavidPr | last post by:
I'm trying to put together a simple job program to use in my community where people can post job ads. It'll cost to place an ad, but I'm having trouble figuring out how to work the paying part. ...
4
by: srblinux | last post by:
Hi all, Where we use Register data type varible in Device drivers? Before the .obj or .exe Generation where we use register data type Declaration.
1
George Lft
by: George Lft | last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And...
65
by: Spiros Bousbouras | last post by:
Has anyone found that declaring variables register affected speed of execution ? If yes on what hardware and around which year ?
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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.