473,800 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using temporary register Variable

I was trying to use a variable whose value is temporary and i don't want
it to go to the memory for performance reasons. Is there a way to specify
the variable like that so that any assignment to the variable does not go
to memory. I tried the type register but it does not seem to work.
Thanks
Shankar
Nov 13 '05 #1
13 6211
Shankar Agarwal <sh******@stanf ord.edu> wrote in
news:Pi******** *************** **************@ cardinal5.Stanf ord.EDU:
I was trying to use a variable whose value is temporary and i don't want
it to go to the memory for performance reasons. Is there a way to
specify the variable like that so that any assignment to the variable
does not go to memory. I tried the type register but it does not seem to
work.


'register' is just a hint, easily ignored by the compiler. Alas, this is
your only standard method to get into a real register. Assembler is your
friend if things are this critical.

--
- Mark ->
--
Nov 13 '05 #2
Greetings.

In article
<Pi************ *************** **********@card inal5.Stanford. EDU>,
Shankar Agarwal wrote:
I was trying to use a variable whose value is temporary and i don't
want it to go to the memory for performance reasons. Is there a way to
specify the variable like that so that any assignment to the variable
does not go to memory.


No, not in standard C. As you've discovered, the "register" specifier
is no more than a hint to the compiler, which it's free to ignore.
(And as most modern compilers are far better at optimizing code than
the average human, they usually do ignore it.) Your compiler may
provide an extension to force register allocation. Is the code really
that performance-critical, though? Optimization should always be done
at the algorithmic level first; worrying about little things like
register variables is rarely worth one's effort.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 13 '05 #3

"Shankar Agarwal" <sh******@stanf ord.edu> wrote in message

I tried the type register but it does not seem to work.

"register" is only a hint. You can try changing compilers, you can try
changing the type of the variable (is it an int?), you can try simplifying
the function it is in and removing other variables, though of course this
might not be possible.
Virtually every C compiler comes with an inline assembler, and you probably
want to resort to this.
Nov 13 '05 #4
Malcolm wrote:

"Shankar Agarwal" <sh******@stanf ord.edu> wrote in message

I tried the type register but it does not seem to work.

"register" is only a hint. You can try changing compilers, you can try
changing the type of the variable (is it an int?), you can try simplifying
the function it is in and removing other variables, though of course this
might not be possible.
Virtually every C compiler comes with an inline assembler, and you probably
want to resort to this.


Ah.. but then it wouldn't be C, would it? Portable, etc.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5

"Joe Wright" <jo********@ear thlink.net> wrote in message
Virtually every C compiler comes with an inline assembler, and you
probably want to resort to this.


Ah.. but then it wouldn't be C, would it? Portable, etc.

What he can do is write two versions of the function, one in C and one using
the assembler. Then use #ifdef [something defined by the compiler which
tells you what it is] to conditionally compile the code.

This isn't a perfect solution, since there's always the risk that a
maintainer might alter one version of the function and not another, breaking
the program. However it's probably the best that can be done.
Nov 13 '05 #6
Joe Wright <jo********@ear thlink.net> wrote in
news:3F******** ***@earthlink.n et on Sat 15 Nov 2003 11:52:02a:
Malcolm wrote:

"Shankar Agarwal" <sh******@stanf ord.edu> wrote in message
>
> I tried the type register but it does not seem to work.
>

"register" is only a hint. You can try changing compilers, you can try
changing the type of the variable (is it an int?), you can try
simplifying the function it is in and removing other variables, though
of course this might not be possible.
Virtually every C compiler comes with an inline assembler, and you
probably want to resort to this.


Ah.. but then it wouldn't be C, would it? Portable, etc.


It's not off-topic to say that you might need to use a non-portable method
to accomplish something, but the person should have the sense not to come
back here to discuss the nonportable aspects of the code (except maybe to
verify that they are, in fact, nonportable).

Nov 13 '05 #7
In <Pi************ *************** **********@card inal5.Stanford. EDU> Shankar Agarwal <sh******@stanf ord.edu> writes:
I was trying to use a variable whose value is temporary and i don't want
it to go to the memory for performance reasons. Is there a way to specify
the variable like that so that any assignment to the variable does not go
to memory. I tried the type register but it does not seem to work.


If your compiler decided to ignore your request for keeping the variable
in a register, it is usually because it has better uses for the available
registers and satisfying your request would result in slower code. Do you
have a good reason for wanting to slow down the generatede code?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

"Dan Pop" <Da*****@cern.c h> wrote in message
If your compiler decided to ignore your request for keeping the
variable in a register, it is usually because it has better uses for the
available registers and satisfying your request would result in slower
code. Do you have a good reason for wanting to slow down the
generatede code?

It's not always as easy as that.

Consider this function.

void child_benefit(S TUDENT *st, int N)
{
int i;
for(i=0;i<N;i++ )
{
for(j=0;j<st[i].nkids;j++)
{
st[i].benefit += 100;
}
}
}

Now as 21st century Westerners we know that N is likely to be large (lots of
students at a univerity) and that 99% of the time the nkids member will be
zero (most students have no children).
Therefore if there is a choice between keeping i in a register and keeping a
register hardwired to 100, we should keep i in the register.
Change the function to "professors " instead of "students" and "number of
lectures" instead of "number of children" and the dynamics chage again -
most universities only have a handful of professors who give quite a lot of
lectures in a year.
The compiler has no way of knowing this, and sometimes the human programmer
does know best.
Nov 13 '05 #9
Malcolm wrote:
It's not always as easy as that.

Consider this function.

void child_benefit(S TUDENT *st, int N)
{
int i;
for(i=0;i<N;i++ )
{
for(j=0;j<st[i].nkids;j++)
{
st[i].benefit += 100;
}
}
}

Now as 21st century Westerners we know that N is likely to be large (lots
of students at a univerity) and that 99% of the time the nkids member will
be zero (most students have no children).
Therefore if there is a choice between keeping i in a register and keeping
a register hardwired to 100, we should keep i in the register.


Just in passing, it seems to me that in this function we can keep all
of N, st, i, and 100 [if we need to] in registers. So it's not a
discriminator.

[Yeah, yeah, I'm stuck on this x86 box, but my heart [1] belongs to the
ARM.]

[1] That part of it that doesn't belong to Renaissance, Reiner Knizia,
Patricia McKillip, and Karnataka.

--
Chris "four chambers is Not Enough!" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #10

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

Similar topics

5
9113
by: Giles Brown | last post by:
I'm feeling quite dumb this morning. I'm trying to build a COM server DLL using py2exe and it ain't working. Here's what ain't working... setup_dll.py based on py2exe sample: """from distutils.core import setup import py2exe
50
17376
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
12
2255
by: Ioannis Vranos | last post by:
Just some thought on it, wanting to see any explanations. It was advised in this newsgroups that we should avoid the use of keyword register. However it is a language feature, and if it offers no help to an implementation, it is free to ignore it. And this happens widely, all my C++ compilers in my platform ignore this keyword.
1
2310
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
20
3504
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
4
1455
by: sanchana | last post by:
how can i check whether a register variable is assigned to a register only or not
7
5168
by: int main(void) | last post by:
Hi all, I know that register variables work fine at block scope. I tried putting a register variable in file scope, like this, #include <stdio.h> register int a = 2; int main(void) { printf("%d\n",a);
25
3777
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce undefined behaviour in c++? The method is: a ^= b ^= a ^= b; It's a clever trick, but I have a feeling something may go wrong if I use it in C++, although quick tests with a few compilers produce correct output. So is the code guaranteed to...
65
2619
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 ?
0
9691
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
10276
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
10253
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,...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
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
6813
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();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.