473,396 Members | 1,766 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 Variable Managment

I apoligise in advance if this is an os or platform based question, I
don't know.

I was wondering how register integers (and other types of register
variables) are managed by c++. For example, on a pentium 4, there are
8 register integers in the cpu. If you define more than 8, or if there
are other programs using this space, how are the variables allocated.
This is for a simulation program that needs to be very fast.
For example:

int foo()
{
register int a;
register int b;
register int c;
register int d;
register int e;
register int f;
register int g;
register int h;
register int i;

//Code using register ints

return 1;
}

There are 9 register integers defined, and, assuming you have only 8 on
your platform, will, after you have defined more than 8, the first one
be put into normal memory or is there some other system for allocating
them.
Also, in for loops, is the counting variable by default a register int.
I have done several tests and the time for a for loop defined like
this:

for( int n = 0; n < 10000; n++ )
{
//Code
}

for( register int n = 0; n < 10000; n++ )
{
//Code
}

is the same.

Thanks
-Alex

Jul 22 '05 #1
3 2234
"Alex" <al*******@gmail.com> wrote...
I apoligise in advance if this is an os or platform based question, I
don't know.
It's not platform-based. It is, however, implementation-defined.
I was wondering how register integers (and other types of register
variables) are managed by c++. For example, on a pentium 4, there are
8 register integers in the cpu. If you define more than 8, or if there
are other programs using this space, how are the variables allocated.
This is for a simulation program that needs to be very fast.
For example:

int foo()
{
register int a;
register int b;
[...]


'register' just like 'inline' is not a directive, but rather a suggestion
for your compiler. It may take a hint and try placing the object in a CPU
register, or it may decide against it and do whatever it sees fit. Just
like you noticed, it's not always possible, besides, it's not always the
desired course of action.

One thing you should probably learn at this point: compilers are better at
deciding what needs to go where than programmers. So, spend your time on
_algorithms_ and getting things _right_, before ever attempting to get them
"very fast". Let the compiler decide what registers to use and when. Do
_not_ concern yourself with performance of your code on any particular CPU
type or model; instead concern yourself with portability and maintainability
of your code. IOW, forget the "register" keyword exists or what it's for,
except that you're not allowed to use it anywhere else. Trust me, your life
will be simpler that way.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
"Alex" <al*******@gmail.com> wrote...
I apoligise in advance if this is an os or platform based question, I
don't know.

It's not platform-based. It is, however, implementation-defined.

I was wondering how register integers (and other types of register
variables) are managed by c++. For example, on a pentium 4, there are
8 register integers in the cpu. If you define more than 8, or if there
are other programs using this space, how are the variables allocated.
This is for a simulation program that needs to be very fast.
For example:

int foo()
{
register int a;
register int b;
[...]

'register' just like 'inline' is not a directive, but rather a suggestion
for your compiler. It may take a hint and try placing the object in a CPU
register, or it may decide against it and do whatever it sees fit. Just
like you noticed, it's not always possible, besides, it's not always the
desired course of action.

One thing you should probably learn at this point: compilers are better at
deciding what needs to go where than programmers. So, spend your time on
_algorithms_ and getting things _right_, before ever attempting to get them
"very fast". Let the compiler decide what registers to use and when. Do
_not_ concern yourself with performance of your code on any particular CPU
type or model; instead concern yourself with portability and maintainability
of your code. IOW, forget the "register" keyword exists or what it's for,
except that you're not allowed to use it anywhere else. Trust me, your life
will be simpler that way.

Victor


However, one can always assist a compiler by writing code that
makes the decisions easier for the compiler.

For example, if a processor has a string search instruction, a
person may want to set up the code so that the compiler recognizes
the "pattern" and implements the string search function.

I've done this on the ARM processor using registers and its
special transfer instructions.

--
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
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #3
Thomas Matthews wrote:
Victor Bazarov wrote:
"Alex" <al*******@gmail.com> wrote...
I apoligise in advance if this is an os or platform based question, I
don't know.


It's not platform-based. It is, however, implementation-defined.

I was wondering how register integers (and other types of register
variables) are managed by c++. For example, on a pentium 4, there are
8 register integers in the cpu. If you define more than 8, or if there
are other programs using this space, how are the variables allocated.
This is for a simulation program that needs to be very fast.
For example:

int foo()
{
register int a;
register int b;
[...]


'register' just like 'inline' is not a directive, but rather a suggestion
for your compiler. It may take a hint and try placing the object in a
CPU
register, or it may decide against it and do whatever it sees fit. Just
like you noticed, it's not always possible, besides, it's not always the
desired course of action.

One thing you should probably learn at this point: compilers are
better at
deciding what needs to go where than programmers. So, spend your time on
_algorithms_ and getting things _right_, before ever attempting to get
them
"very fast". Let the compiler decide what registers to use and when. Do
_not_ concern yourself with performance of your code on any particular
CPU
type or model; instead concern yourself with portability and
maintainability
of your code. IOW, forget the "register" keyword exists or what it's
for,
except that you're not allowed to use it anywhere else. Trust me,
your life
will be simpler that way.

Victor


However, one can always assist a compiler by writing code that
makes the decisions easier for the compiler.

For example, if a processor has a string search instruction, a
person may want to set up the code so that the compiler recognizes
the "pattern" and implements the string search function.

I've done this on the ARM processor using registers and its
special transfer instructions.


Oh, there is no doubt about it. The point[s] of my post was/were that
(a) 'register' is not the way to do that, (b) before attempting doing that
one should understand the inner workings of the compiler/CPU combination
very well and (c) portability and maintainability are often more important
than minute improvements in performance: productivity is not measured by
the performance of a single program fragment but by the overall output of
the [sometimes quite large] group of people that involves using and
advancing that program (and I am not sure if I read it somewhere or that
I just made it up :-).

Victor
Jul 22 '05 #4

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

Similar topics

12
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...
10
by: ajay | last post by:
some say floats can't be stored in register. size of int and float are same ( on some machine am i Wrong?) so if int can be delcared as register than why not float(where in and float are of same...
13
by: Shankar Agarwal | last post by:
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...
14
by: aruna | last post by:
What is the disadvantage of using register storage class specifier?
9
by: Jackie | last post by:
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
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 ?
4
by: sanchana | last post by:
how can i check whether a register variable is assigned to a register only or not
29
by: orium69 | last post by:
hi everyone, i'm wondering if there is a way to have sure that a variable is allocated in the cache, after its declaration with "register"? Tks!
7
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) {...
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
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
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
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...
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.