Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3830 > unrolled thread

Register storage type

Started byFree Willy <nospam@nospam.com>
First post2011-05-12 20:55 +0000
Last post2011-05-12 17:39 -0700
Articles 20 on this page of 42 — 20 participants

Back to article view | Back to comp.lang.c


Contents

  Register storage type Free Willy <nospam@nospam.com> - 2011-05-12 20:55 +0000
    Re: Register storage type Kenneth Brody <kenbrody@spamcop.net> - 2011-05-12 17:02 -0400
      Re: Register storage type Free Willy <nospam@nospam.com> - 2011-05-12 21:13 +0000
        Re: Register storage type Angel <angel+news@spamcop.net> - 2011-05-12 21:20 +0000
        Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-12 14:27 -0700
          Re: Register storage type Kenneth Brody <kenbrody@spamcop.net> - 2011-05-13 13:20 -0400
            Re: Register storage type Free Willy <nospam@nospam.com> - 2011-05-13 21:30 +0000
              Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-13 15:50 -0700
                Different languages (Was: Register storage type) gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-14 08:58 +0000
                  Re: Different languages (Was: Register storage type) Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-14 10:09 +0100
                Re: Register storage type Free Willy <nospam@nospam.com> - 2011-05-14 21:27 +0000
                  Re: Register storage type Ian Collins <ian-news@hotmail.com> - 2011-05-15 09:55 +1200
                  Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-14 16:46 -0700
                    Re: Register storage type Jens <jens.gustedt@gmail.com> - 2011-05-15 10:08 -0700
                      Re: Register storage type James Kuyper <jameskuyper@verizon.net> - 2011-05-15 14:09 -0400
                    Re: Register storage type David Thompson <dave.thompson2@verizon.net> - 2011-05-19 01:50 -0400
                  Re: Register storage type Stephen Sprunk <stephen@sprunk.org> - 2011-05-15 00:27 -0500
                  Re: Register storage type Angel <angel+news@spamcop.net> - 2011-05-15 12:56 +0000
                    Re: Register storage type Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-15 11:54 -0400
                      Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-15 17:43 +0000
                        Re: Register storage type Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-18 23:37 -0700
                          Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-19 14:37 +0000
                            Drift: Linguistics (was Re: Register storage type) Kenneth Brody <kenbrody@spamcop.net> - 2011-05-19 13:17 -0400
                            Re: Register storage type Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-19 11:05 -0700
                              Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-19 19:08 +0000
                                Re: Register storage type Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-24 00:39 -0700
                              Re: Register storage type Ian Collins <ian-news@hotmail.com> - 2011-05-20 07:46 +1200
                              Re: Register storage type Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-19 22:37 -0400
                            Re: Register storage type Michael Press <rubrum@pacbell.net> - 2011-05-25 12:57 -0700
                    Re: Register storage type pete <pfiland@mindspring.com> - 2011-05-15 09:09 -0400
        Re: Register storage type Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-12 21:57 -0400
    Re: Register storage type Angel <angel+news@spamcop.net> - 2011-05-12 21:14 +0000
      Re: Register storage type gordonb.unc3q@burditt.org (Gordon Burditt) - 2011-05-13 07:49 -0500
    Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-12 14:20 -0700
      Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-12 21:59 +0000
        Re: Register storage type BGB <cr88192@hotmail.com> - 2011-05-12 15:50 -0700
        Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-12 15:49 -0700
          Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-12 23:19 +0000
            Re: Register storage type Keith Thompson <kst-u@mib.org> - 2011-05-12 16:29 -0700
              Re: Register storage type Seebs <usenet-nospam@seebs.net> - 2011-05-13 00:15 +0000
    Re: Register storage type Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 15:24 -0700
    Re: Register storage type Peter Nilsson <airia@acay.com.au> - 2011-05-12 17:39 -0700

Page 1 of 3  [1] 2 3  Next page →


#3830 — Register storage type

FromFree Willy <nospam@nospam.com>
Date2011-05-12 20:55 +0000
SubjectRegister storage type
Message-ID<iqhhgg$d8j$1@speranza.aioe.org>
Hello

Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
want to declare 10 register variables in my code, is it possible to do
it?

Say size_of(float) is the same as size_of(int), is it possible to define 
a "register float" variable? How will the register get converted between 
int and float?

Thanks.

[toc] | [next] | [standalone]


#3832

FromKenneth Brody <kenbrody@spamcop.net>
Date2011-05-12 17:02 -0400
Message-ID<OeGdnRorOK1J11HQnZ2dnUVZ_gSdnZ2d@bestweb.net>
In reply to#3830
On 5/12/2011 4:55 PM, Free Willy wrote:
> Hello
>
> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
> want to declare 10 register variables in my code, is it possible to do
> it?
>
> Say size_of(float) is the same as size_of(int), is it possible to define
> a "register float" variable? How will the register get converted between
> int and float?

The "register" keyword is just a hint to the compiler, which it is free to 
use or ignore as it sees fit.

-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#3833

FromFree Willy <nospam@nospam.com>
Date2011-05-12 21:13 +0000
Message-ID<iqhiie$g5j$1@speranza.aioe.org>
In reply to#3832
Kenneth Brody writes:

> On 5/12/2011 4:55 PM, Free Willy wrote:
>> Hello
>>
>> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
>> want to declare 10 register variables in my code, is it possible to do
>> it?
>>
>> Say size_of(float) is the same as size_of(int), is it possible to
>> define a "register float" variable? How will the register get converted
>> between int and float?
> 
> The "register" keyword is just a hint to the compiler, which it is free
> to use or ignore as it sees fit.

OK, but will it always give a warning if you try to assign too many 
register variables for the CPU (or the wrong type of variable, if this 
isn't supported), or just compile it possibly with errors or undefined 
behaviors at runtime?

Another question is array variables declared as register type - could the 
compiler put a small array say of 3 elements into different registers and 
translate array accesses to the right register?

[toc] | [prev] | [next] | [standalone]


#3837

FromAngel <angel+news@spamcop.net>
Date2011-05-12 21:20 +0000
Message-ID<slrnisojok.5v3.angel+news@pearlgates.net>
In reply to#3833
On 2011-05-12, Free Willy <nospam@nospam.com> wrote:
> Kenneth Brody writes:
>
>> The "register" keyword is just a hint to the compiler, which it is free
>> to use or ignore as it sees fit.
>
> OK, but will it always give a warning if you try to assign too many 
> register variables for the CPU (or the wrong type of variable, if this 
> isn't supported), or just compile it possibly with errors or undefined 
> behaviors at runtime?

Since it's just a hint, no warnings will be given if the compiler
cannot, or chooses not to follow it. This will not lead to errors or
undefined behaviour; the variables will simply be treated like all
others.

> Another question is array variables declared as register type - could the 
> compiler put a small array say of 3 elements into different registers and 
> translate array accesses to the right register?

I might, if doing so is feasible. The language makes no guarantee
either way, it's up to the compiler to decide.


-- 
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

[toc] | [prev] | [next] | [standalone]


#3839

FromKeith Thompson <kst-u@mib.org>
Date2011-05-12 14:27 -0700
Message-ID<lnaaerk3sg.fsf@nuthaus.mib.org>
In reply to#3833
Free Willy <nospam@nospam.com> writes:
> Kenneth Brody writes:
>> On 5/12/2011 4:55 PM, Free Willy wrote:
>>> Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
>>> want to declare 10 register variables in my code, is it possible to do
>>> it?
>>>
>>> Say size_of(float) is the same as size_of(int), is it possible to
>>> define a "register float" variable? How will the register get converted
>>> between int and float?
>> 
>> The "register" keyword is just a hint to the compiler, which it is free
>> to use or ignore as it sees fit.
>
> OK, but will it always give a warning if you try to assign too many 
> register variables for the CPU (or the wrong type of variable, if this 
> isn't supported), or just compile it possibly with errors or undefined 
> behaviors at runtime?

A compiler can warn about anything it likes, and some might give such a
warning, either by default or by the use of some command-line option.
But no such warning is required, and as far as I know most compilers
won't bother.

Declaring "too many" register variables won't cause errors or undefined
behavior.  The compiler isn't disobeying the "register" specifier, since
the specifier (in spite of the name) doesn't say to store the variable
in a register.

> Another question is array variables declared as register type - could the 
> compiler put a small array say of 3 elements into different registers and 
> translate array accesses to the right register?

If you declare an array variable as "register", you can't take its
address.  Since array indexing is defined in terms of addresses, such an
array variable is pretty much useless.

If you declare an array variable *without* "register", the compiler can
store it in one or more registers, as long as the behavior of the
program is not affected.  For example, if you take the address of one of
the elements of the array and pass it to some function, the compiler
isn't likely to be able to store it in a register, since the called
function woudnl't know how to access it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#3909

FromKenneth Brody <kenbrody@spamcop.net>
Date2011-05-13 13:20 -0400
Message-ID<_ZednX81cqTb9VDQnZ2dnUVZ_s-dnZ2d@bestweb.net>
In reply to#3839
On 5/12/2011 5:27 PM, Keith Thompson wrote:
> Free Willy<nospam@nospam.com>  writes:
[...]
>> Another question is array variables declared as register type - could the
>> compiler put a small array say of 3 elements into different registers and
>> translate array accesses to the right register?
>
> If you declare an array variable as "register", you can't take its
> address.  Since array indexing is defined in terms of addresses, such an
> array variable is pretty much useless.

And can lead to some "interesting" errors:

     ...
     register int foo[3];
     foo[0] = 1;
     ...

This causes the error "'&' on register variable".

(Yes, I know _why_ it says that, but it is a bit unexpected at first glance.)

[...]

-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#3919

FromFree Willy <nospam@nospam.com>
Date2011-05-13 21:30 +0000
Message-ID<iqk7t1$u3f$1@speranza.aioe.org>
In reply to#3909
Kenneth Brody writes:
> On 5/12/2011 5:27 PM, Keith Thompson wrote:
>> Free Willy<nospam@nospam.com>  writes:
> [...]
>>> Another question is array variables declared as register type - could
>>> the compiler put a small array say of 3 elements into different
>>> registers and translate array accesses to the right register?
>>
>> If you declare an array variable as "register", you can't take its
>> address.  Since array indexing is defined in terms of addresses, such
>> an array variable is pretty much useless.
> 
> And can lead to some "interesting" errors:
> 
>      ...
>      register int foo[3];
>      foo[0] = 1;
>      ...
> 
> This causes the error "'&' on register variable".
> 
> (Yes, I know _why_ it says that, but it is a bit unexpected at first
> glance.)
> 
> [...]

This is interesting, I usually use g++ which compiles the code without 
any warnings, but gcc throws a compile error!

[toc] | [prev] | [next] | [standalone]


#3921

FromKeith Thompson <kst-u@mib.org>
Date2011-05-13 15:50 -0700
Message-ID<ln1v02i5az.fsf@nuthaus.mib.org>
In reply to#3919
Free Willy <nospam@nospam.com> writes:
> Kenneth Brody writes:
>> On 5/12/2011 5:27 PM, Keith Thompson wrote:
>>> Free Willy<nospam@nospam.com>  writes:
>> [...]
>>>> Another question is array variables declared as register type - could
>>>> the compiler put a small array say of 3 elements into different
>>>> registers and translate array accesses to the right register?
>>>
>>> If you declare an array variable as "register", you can't take its
>>> address.  Since array indexing is defined in terms of addresses, such
>>> an array variable is pretty much useless.
>> 
>> And can lead to some "interesting" errors:
>> 
>>      ...
>>      register int foo[3];
>>      foo[0] = 1;
>>      ...
>> 
>> This causes the error "'&' on register variable".
>> 
>> (Yes, I know _why_ it says that, but it is a bit unexpected at first
>> glance.)
>> 
>> [...]
>
> This is interesting, I usually use g++ which compiles the code without 
> any warnings, but gcc throws a compile error!

Always remember that C and C++ are two different languages.  The C++
semantics for "register" differ from the C semantics.

<OT>
C++ 2003 7.1.1p3:
    A register specifier has the same semantics as an auto specifier
    together with a hint to the implementation that the object so
    declared will be heavily used. [Note: the hint can be ignored
    and in most implementations it will be ignored if the address
    of the object is taken. —end note]
</OT>

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#3935 — Different languages (Was: Register storage type)

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2011-05-14 08:58 +0000
SubjectDifferent languages (Was: Register storage type)
Message-ID<iqlg81$dor$2@news.xmission.com>
In reply to#3921
In article <ln1v02i5az.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> bellowed for the umpteenth time:
...
>Always remember that C and C++ are two different languages.  The C++

Perl 3 and Perl 4 and Perl 5 are all different languages, too.

So what?

(Yes, there is a point in all of this, but I'm too lazy to write it all out
right now.  If you're smart, you'll get it...)

-- 
One of the best lines I've heard lately:

    Obama could cure cancer tomorrow, and the Republicans would be
    complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it.  We've constructed an economy in which eliminating
cancer would be a horrible disaster.  There are many other such examples.)

[toc] | [prev] | [next] | [standalone]


#3936 — Re: Different languages (Was: Register storage type)

FromDr Nick <3-nospam@temporary-address.org.uk>
Date2011-05-14 10:09 +0100
SubjectRe: Different languages (Was: Register storage type)
Message-ID<87k4dtd4xg.fsf@temporary-address.org.uk>
In reply to#3935
gazelle@shell.xmission.com (Kenny McCormack) writes:

> In article <ln1v02i5az.fsf@nuthaus.mib.org>,
> Keith Thompson  <kst-u@mib.org> bellowed for the umpteenth time:
> ...
>>Always remember that C and C++ are two different languages.  The C++
>
> Perl 3 and Perl 4 and Perl 5 are all different languages, too.
>
> So what?
>
> (Yes, there is a point in all of this, but I'm too lazy to write it all out
> right now.  If you're smart, you'll get it...)

Just before I'll pop you back in my killfile, I'll ask you to explain
the point.  Because this wasn't a gratuitous pop at C/C++ it was an
important explanation of why something worked in one and not in the
other, and not because of a new feature, but because the semantics of a
keyword have subtly changed.

I have to say, for a troll you have astonishing patience, but surely
there's something more productive you could do with the hours and hours
you spend on this.
-- 
Online waterways route planner            | http://canalplan.eu
Plan trips, see photos, check facilities  | http://canalplan.org.uk

[toc] | [prev] | [next] | [standalone]


#3983

FromFree Willy <nospam@nospam.com>
Date2011-05-14 21:27 +0000
Message-ID<iqms36$vj1$1@speranza.aioe.org>
In reply to#3921
Keith Thompson writes:
> Free Willy <nospam@nospam.com> writes:
>> Kenneth Brody writes:
>>> On 5/12/2011 5:27 PM, Keith Thompson wrote:
>>>> Free Willy<nospam@nospam.com>  writes:
>>> [...]
>>>>> Another question is array variables declared as register type -
>>>>> could the compiler put a small array say of 3 elements into
>>>>> different registers and translate array accesses to the right
>>>>> register?
>>>>
>>>> If you declare an array variable as "register", you can't take its
>>>> address.  Since array indexing is defined in terms of addresses, such
>>>> an array variable is pretty much useless.
>>> 
>>> And can lead to some "interesting" errors:
>>> 
>>>      ...
>>>      register int foo[3];
>>>      foo[0] = 1;
>>>      ...
>>> 
>>> This causes the error "'&' on register variable".
>>> 
>>> (Yes, I know _why_ it says that, but it is a bit unexpected at first
>>> glance.)
>>> 
>>> [...]
>>
>> This is interesting, I usually use g++ which compiles the code without
>> any warnings, but gcc throws a compile error!
> 
> Always remember that C and C++ are two different languages.  The C++
> semantics for "register" differ from the C semantics.
> 
> <OT>
> C++ 2003 7.1.1p3:
>     A register specifier has the same semantics as an auto specifier
>     together with a hint to the implementation that the object so
>     declared will be heavily used. [Note: the hint can be ignored and in
>     most implementations it will be ignored if the address of the object
>     is taken. —end note]
> </OT>

That's interesting, although I don't think it really explains it - I 
normally use g++ in C mode because it gives some extra type checking, eg 
prevents implicit typecasting of pointers.

Personally I think the C++ way is better than the C way - it avoids 
problems if you try to assign more register variables than the machine 
has registers available, effectively in C++ it sounds like the compiler 
can "fall back" to discarding the register specification and treating it 
like a normal stack variable should there be no register available.

I use the register keyword to tell the compiler that a variable is used a 
lot eg as a loop index. What I "really want" is for the compiler to 
optimize access to that variable - putting it in a register is only a 
means to an end!

[toc] | [prev] | [next] | [standalone]


#3985

FromIan Collins <ian-news@hotmail.com>
Date2011-05-15 09:55 +1200
Message-ID<938c2nFoqnU1@mid.individual.net>
In reply to#3983
On 05/15/11 09:27 AM, Free Willy wrote:
> Keith Thompson writes:
>> Free Willy<nospam@nospam.com>  writes:
>>>
>>> This is interesting, I usually use g++ which compiles the code without
>>> any warnings, but gcc throws a compile error!
>>
>> Always remember that C and C++ are two different languages.  The C++
>> semantics for "register" differ from the C semantics.
>>
>> <OT>
>> C++ 2003 7.1.1p3:
>>      A register specifier has the same semantics as an auto specifier
>>      together with a hint to the implementation that the object so
>>      declared will be heavily used. [Note: the hint can be ignored and in
>>      most implementations it will be ignored if the address of the object
>>      is taken. —end note]
>> </OT>
>
> That's interesting, although I don't think it really explains it - I
> normally use g++ in C mode because it gives some extra type checking, eg
> prevents implicit typecasting of pointers.
>
> Personally I think the C++ way is better than the C way - it avoids
> problems if you try to assign more register variables than the machine
> has registers available, effectively in C++ it sounds like the compiler
> can "fall back" to discarding the register specification and treating it
> like a normal stack variable should there be no register available.

The easiest solution is to not use the register keyword.  It's an 
anachronism with modern compilers.

> I use the register keyword to tell the compiler that a variable is used a
> lot eg as a loop index. What I "really want" is for the compiler to
> optimize access to that variable - putting it in a register is only a
> means to an end!

The compiler can perform the required optimisations on its own!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#3990

FromKeith Thompson <kst-u@mib.org>
Date2011-05-14 16:46 -0700
Message-ID<lnfwoghml4.fsf@nuthaus.mib.org>
In reply to#3983
Free Willy <nospam@nospam.com> writes:
> Keith Thompson writes:
[...]
>> Always remember that C and C++ are two different languages.  The C++
>> semantics for "register" differ from the C semantics.
>> 
>> <OT>
>> C++ 2003 7.1.1p3:
>>     A register specifier has the same semantics as an auto specifier
>>     together with a hint to the implementation that the object so
>>     declared will be heavily used. [Note: the hint can be ignored and in
>>     most implementations it will be ignored if the address of the object
>>     is taken. —end note]
>> </OT>
>
> That's interesting, although I don't think it really explains it - I 
> normally use g++ in C mode because it gives some extra type checking, eg 
> prevents implicit typecasting of pointers.

I don't believe g++ has a C mode.  g++ and gcc are part of the same
compiler collection; g++ is the C++ compiler, and gcc is the C compiler.

I suppose you mean that you use g++ to compile C code, but as you've
seen, this doesn't always work.

(OH, and you mean "implicit conversions".  Casts are always explicit.)

> Personally I think the C++ way is better than the C way - it avoids 
> problems if you try to assign more register variables than the machine 
> has registers available, effectively in C++ it sounds like the compiler 
> can "fall back" to discarding the register specification and treating it 
> like a normal stack variable should there be no register available.
>
> I use the register keyword to tell the compiler that a variable is used a 
> lot eg as a loop index. What I "really want" is for the compiler to 
> optimize access to that variable - putting it in a register is only a 
> means to an end!

"register" is a relic of earlier times with less intelligent optimizing
compilers.  The compiler can figure out for itself that a variable is
used heavily, especially a local variable (which is all you can apply
"register" to anyway).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#4027

FromJens <jens.gustedt@gmail.com>
Date2011-05-15 10:08 -0700
Message-ID<4b4cfc35-ec1f-42cc-bd0a-cd3c7d76b470@z37g2000vbl.googlegroups.com>
In reply to#3990
On 15 Mai, 01:46, Keith Thompson <ks...@mib.org> wrote:
> "register" is a relic of earlier times with less intelligent optimizing
> compilers.  The compiler can figure out for itself that a variable is
> used heavily, especially a local variable (which is all you can apply
> "register" to anyway).

I think everybody here gets the sense of register the wrong way. It is
a contract with the compiler that we will not take the address of a
variable and that we ask him to tell us when we do so. So this is a
kind of compile time assertion, not more and not less.

[toc] | [prev] | [next] | [standalone]


#4029

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-05-15 14:09 -0400
Message-ID<iqp4s6$1mk$2@dont-email.me>
In reply to#4027
On 05/15/2011 01:08 PM, Jens wrote:
> On 15 Mai, 01:46, Keith Thompson <ks...@mib.org> wrote:
>> "register" is a relic of earlier times with less intelligent optimizing
>> compilers. �The compiler can figure out for itself that a variable is
>> used heavily, especially a local variable (which is all you can apply
>> "register" to anyway).
> 
> I think everybody here gets the sense of register the wrong way. It is
> a contract with the compiler that we will not take the address of a
> variable and that we ask him to tell us when we do so. So this is a
> kind of compile time assertion, not more and not less.

That's the only practical effect of 'register', but that's not it's
intended purpose.

-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#4291

FromDavid Thompson <dave.thompson2@verizon.net>
Date2011-05-19 01:50 -0400
Message-ID<ejb9t617uelm0na0uk879vg8v07jaftua3@4ax.com>
In reply to#3990
On Sat, 14 May 2011 16:46:47 -0700, Keith Thompson <kst-u@mib.org>
wrote:

> Free Willy <nospam@nospam.com> writes:
<snip: 'register' in C versus C++>
> > That's interesting, although I don't think it really explains it - I 
> > normally use g++ in C mode because it gives some extra type checking, eg 
> > prevents implicit typecasting of pointers.
> 
> I don't believe g++ has a C mode.  g++ and gcc are part of the same
> compiler collection; g++ is the C++ compiler, and gcc is the C compiler.
> 
Actually both 'gcc' and 'g++' are only the toplevel drivers, although
they often come packaged with particular underlying compilers 
(and sometimes with binutils as well). In particular, *both* 'gcc' and
'g++' can *compile* C or C++ or a mixture; in fact they can also
*compile* ObjC, Fortran (77+ or ~95 depending on version), and Ada if
those compilers are included in the build/package(s).

The difference is in linking (or equivalent): any driver can link C,
but only 'g++' does the additional libraries etc. for C++, only
'gfortran' does Fortran, only 'gnatbind' does Ada. If you want to
combine say Fortan + C++ (+ C) you have to fiddle it yourself.

The decision which compiler to use for each sourcefile is normally
based on the extension (you can override with -x) and on Windows with
case-insensitive filenames the treatment of .c varies -- at least for
mingw; I believe the same is true for cygwin but haven't tried it.

> I suppose you mean that you use g++ to compile C code, but as you've
> seen, this doesn't always work.
> 
So on Windows g++ foo.c actually compiles as C++. If that source is in
the 'better C' subset of C++, it works and uses C++ rules. As has been
repeatedly discussed here, that subset is not all of C, but it is
quite enough to IMO-reasonably handle any IMO-reasonable program.

> (OH, and you mean "implicit conversions".  Casts are always explicit.)
> 

[toc] | [prev] | [next] | [standalone]


#3997

FromStephen Sprunk <stephen@sprunk.org>
Date2011-05-15 00:27 -0500
Message-ID<iqno7d$vd4$1@dont-email.me>
In reply to#3983
On 14-May-11 16:27, Free Willy wrote:
> Keith Thompson writes:
>> Always remember that C and C++ are two different languages.  The C++
>> semantics for "register" differ from the C semantics.
>>
>> <OT>
>> C++ 2003 7.1.1p3:
>>     A register specifier has the same semantics as an auto specifier
>>     together with a hint to the implementation that the object so
>>     declared will be heavily used. [Note: the hint can be ignored and in
>>     most implementations it will be ignored if the address of the object
>>     is taken. —end note]
>> </OT>
> 
> That's interesting, although I don't think it really explains it - I 
> normally use g++ in C mode because it gives some extra type checking, eg 
> prevents implicit typecasting of pointers.

"Typecasting" is something done to actors, not pointers.

There is no such thing as an "implicit cast"; all casts are explicit
(conversions).  ITYM "implicit conversion".

> Personally I think the C++ way is better than the C way - it avoids 
> problems if you try to assign more register variables than the machine 
> has registers available, effectively in C++ it sounds like the compiler 
> can "fall back" to discarding the register specification and treating it 
> like a normal stack variable should there be no register available.

In that respect, there is no difference.  The "register" class is just a
hint in both languages.  The compiler is free to ignore it when
allocating registers--if the target even _has_ registers.

The only difference I can see, based on the text quoted above, is that
taking the address of a register variable in C++ implicitly changes it
to an auto variable but is an error in C.

> I use the register keyword to tell the compiler that a variable is used a 
> lot eg as a loop index. What I "really want" is for the compiler to 
> optimize access to that variable - putting it in a register is only a 
> means to an end!

Modern compilers should figure that out on their own.  If your doesn't,
you would be better served by getting a better one than by cluttering
your code with micro-optimizations that make it harder for your primary
audience, i.e. humans, to understand.

S

-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


#4016

FromAngel <angel+news@spamcop.net>
Date2011-05-15 12:56 +0000
Message-ID<slrnisvjb4.34t.angel+news@pearlgates.net>
In reply to#3983
On 2011-05-14, Free Willy <nospam@nospam.com> wrote:
>
> Personally I think the C++ way is better than the C way - it avoids 
> problems if you try to assign more register variables than the machine 
> has registers available, effectively in C++ it sounds like the compiler 
> can "fall back" to discarding the register specification and treating it 
> like a normal stack variable should there be no register available.

You weren't paying attention earlier. The compiler can and will ignore
the "register" specification _whenever_ _it_ _wants_. There is no such
thing as running out of registers, and no bad stuff will happen if there
are less registers than there are variables with the "register"
specification.

> I use the register keyword to tell the compiler that a variable is used a 
> lot eg as a loop index. What I "really want" is for the compiler to 
> optimize access to that variable - putting it in a register is only a 
> means to an end!

Why are you so focussed on these micro-optimizations anyway? Let the
compiler figure it out, modern compilers are smart enough to do it on
their own for all but a few very limited cases.


-- 
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

[toc] | [prev] | [next] | [standalone]


#4022

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-05-15 11:54 -0400
Message-ID<iqosvm$eap$1@dont-email.me>
In reply to#4016
On 5/15/2011 8:56 AM, Angel wrote:
> On 2011-05-14, Free Willy<nospam@nospam.com>  wrote:
>>
>> Personally I think the C++ way is better than the C way - it avoids
>> problems if you try to assign more register variables than the machine
>> has registers available, effectively in C++ it sounds like the compiler
>> can "fall back" to discarding the register specification and treating it
>> like a normal stack variable should there be no register available.
>
> You weren't paying attention earlier. The compiler can and will ignore
> the "register" specification _whenever_ _it_ _wants_. There is no such
> thing as running out of registers, and no bad stuff will happen if there
> are less registers than there are variables with the "register"
> specification.

     Back in the day when compilers paid serious attention to `register',
"bad stuff" could indeed happen.  I recall two unpleasantesses:

     - The compiler might dutifully dedicate CPU registers to variables,
       making them unavailable for other uses.  This could cause even
       fairly simple expressions to fail to compile, if too few CPU
       registers remained to hold the intermediate results.

     - The compiler might generate extra code to save and restore the
       contents of registers dedicated to variables, or to deal with
       register scarcity for intermediate expression results.  This
       could easily slow things down by more than register-residence
       for the variables could speed them up.

     When compilers had to run in limited memory on slow machines,
they could not afford the kind of analysis they've been doing as a
matter of routine for the last twenty or twenty-five years.  In those
days, `register' was sometimes helpful -- although even then it was
quite tricky to employ it well.  It was a portable construct with a
non-portable effect.

>> I use the register keyword to tell the compiler that a variable is used a
>> lot eg as a loop index. What I "really want" is for the compiler to
>> optimize access to that variable - putting it in a register is only a
>> means to an end!
>
> Why are you so focussed on these micro-optimizations anyway? Let the
> compiler figure it out, modern compilers are smart enough to do it on
> their own for all but a few very limited cases.

     He's already been told this, by multiple people, but he's not
listening.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#4030

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-15 17:43 +0000
Message-ID<slrnit03pp.8l9.usenet-nospam@guild.seebs.net>
In reply to#4022
On 2011-05-15, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>      Back in the day when compilers paid serious attention to `register',
> "bad stuff" could indeed happen.  I recall two unpleasantesses:

>      - The compiler might dutifully dedicate CPU registers to variables,
>        making them unavailable for other uses.  This could cause even
>        fairly simple expressions to fail to compile, if too few CPU
>        registers remained to hold the intermediate results.

In fact, I've seen a modern compiler do this within the last couple of
years, though it was from something a bit more elaborate than just
"register".  There exist circumstances under which one popular C library
can be induced to cause horrible compiler failures on x86 because it inlines
string routines in ways that chew up registers.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web