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


Groups > comp.lang.c > #3983

Re: Register storage type

From Free Willy <nospam@nospam.com>
Newsgroups comp.lang.c
Subject Re: Register storage type
Date 2011-05-14 21:27 +0000
Organization Aioe.org NNTP Server
Message-ID <iqms36$vj1$1@speranza.aioe.org> (permalink)
References (2 earlier) <iqhiie$g5j$1@speranza.aioe.org> <lnaaerk3sg.fsf@nuthaus.mib.org> <_ZednX81cqTb9VDQnZ2dnUVZ_s-dnZ2d@bestweb.net> <iqk7t1$u3f$1@speranza.aioe.org> <ln1v02i5az.fsf@nuthaus.mib.org>

Show all headers | View raw


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!

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web