Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Casts on lvalues |
| Date | 2012-12-07 15:41 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <50C2547E.30609@verizon.net> (permalink) |
| References | (12 earlier) <ftmws.280907$kt5.95535@fx08.am4> <50C216B3.60103@verizon.net> <3zqws.788609$vW7.180152@fx19.am4> <50C24019.1020505@verizon.net> <Z8sws.791951$A%.338357@fx26.am4> |
On 12/07/2012 03:16 PM, BartC wrote:
> "James Kuyper" <jameskuyper@verizon.net> wrote in message
> news:50C24019.1020505@verizon.net...
...
>> I'm not talking about all optimizations, just the ones that depend upon
>> C's anti-aliasing guarantees. Consider the following function:
>>
>> void func(int n, float array[static n], long *l)
>> {
>> for(int i=0; i<n; i++)
>> array[i] = *l;
>> *l = n;
>> }
>
>> An implementation is permitted, because of 6.5p7, to compile that
>> function as if it had been written:
>
>> void func(int n, float array[static n], long *l)
>> {
>> long temp = *l;
>> for(int i=0; i<n; i++)
>> array[i] = temp;
>> *l = n;
>> }
>>
>> The optimized version has exactly the same behavior as the original, so
>> long as 6.5p7 is not violated. If C were changed to allow a long integer
>> to be equivalenced with a element of a float array, the value of *l
>> might change during the loop.
>
> (Seems like it might just be copied to itself? In this example).
I think you're assuming that sizeof(long)==sizeof(float), which is
commonplace, but not required. However, even on systems where that is
true, the result is not a simple copy. The conversion to float will
produce a bit pattern that's pretty unlikely to be the same as the
original (unless the original represents 0).
> >If so, these two versions of the code
>> would no longer be equivalent, and there's an important question to be
>> asked about which one actually matches the user's intent. If the initial
>> value of *l was carefully chosen with the intent that it would, in fact,
>> be changed by this code inside that loop, the developer is going to be a
>> little upset if that didn't actually happen.
>
> I can't see that the problems are going to be that different from the ones
> you might get when using pointers. (And the solutions might be similar.)
In C, such code using pointer casts violates 6.5p7, and therefore has
undefined behavior, so the solution is simply not to write such code.
> Because even without equivalencing, 'l' might anyway point into the array,
> via a cast.
Not with defined behavior.
> If this was part of the language, at least the aliasing would be done in an
> overt manner, not hidden away in a pointer cast, in an operation done at
> runtime so you don't know at any time what is aliasing what.
As I said, the C language does provide a way to explicitly alias objects
of two different types, and the optimizations I've described above are
therefore prohibited when they occur within the scope of a union
declaration that connects the relevant types (except in those contexts
where the compiler can be certain that the relevant pointers do not
point at members of the same union object).
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-02 13:23 +0000
Re: Casts on lvalues SG <sgesemann@gmail.invalid> - 2012-12-02 14:40 +0100
Re: Casts on lvalues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-02 13:53 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-06 12:26 +0000
Re: Casts on lvalues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-06 15:15 +0000
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-06 12:05 -0800
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-02 12:34 -0800
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-02 21:18 +0000
Re: Casts on lvalues Willem <willem@turtle.stack.nl> - 2012-12-05 22:36 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 23:46 +0000
Re: Casts on lvalues Willem <willem@turtle.stack.nl> - 2012-12-06 18:35 +0000
Re: Casts on lvalues pete <pfiland@mindspring.com> - 2012-12-02 16:21 -0500
Re: Casts on lvalues Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-02 17:10 -0500
Re: Casts on lvalues pete <pfiland@mindspring.com> - 2012-12-02 18:50 -0500
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-02 19:14 -0500
Re: Casts on lvalues falk@rahul.net (Edward A. Falk) - 2012-12-04 23:57 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 01:35 +0000
Re: Casts on lvalues Piotr Kalinowski <pitkali@gmail.com> - 2012-12-05 16:07 +0100
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 16:47 +0000
Re: Casts on lvalues Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-05 13:04 -0500
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 18:39 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-05 14:23 -0500
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 22:11 +0000
Re: Casts on lvalues Ian Collins <ian-news@hotmail.com> - 2012-12-06 11:51 +1300
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 23:58 +0000
Re: Casts on lvalues Piotr Kalinowski <pitkali@gmail.com> - 2012-12-06 22:00 +0100
Re: Casts on lvalues glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-06 03:05 +0000
Re: Casts on lvalues Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-05 14:43 -0500
Re: Casts on lvalues Greg Martin <greg@softsprocket.com> - 2012-12-05 12:25 -0800
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 21:57 +0000
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-05 16:09 -0800
Re: Casts on lvalues Philipp Thomas <kthomas@linux01.gwdg.de> - 2012-12-07 01:24 +0100
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-06 17:22 -0800
Re: Casts on lvalues Öö Tiib <ootiib@hot.ee> - 2012-12-07 00:31 -0800
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-07 08:18 -0500
Re: Casts on lvalues Öö Tiib <ootiib@hot.ee> - 2012-12-07 09:07 -0800
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-07 11:01 -0800
Re: Casts on lvalues glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-07 21:57 +0000
Re: Casts on lvalues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-07 22:29 +0000
Re: Casts on lvalues glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-08 00:04 +0000
Re: Casts on lvalues lawrence.jones@siemens.com - 2012-12-15 16:51 -0500
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-15 15:48 -0800
Re: Casts on lvalues lawrence.jones@siemens.com - 2012-12-17 16:37 -0500
Re: Casts on lvalues Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 02:58 -0800
Re: Casts on lvalues Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 02:53 -0800
Re: Casts on lvalues Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 02:43 -0800
Re: Casts on lvalues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-05 22:07 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-05 22:20 +0000
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-05 16:10 -0800
Re: Casts on lvalues Philip Lantz <prl@canterey.us> - 2012-12-06 22:49 -0800
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 12:54 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-07 08:22 -0500
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 13:48 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-07 11:17 -0500
Re: Casts on lvalues glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-07 17:33 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 18:28 +0000
Re: Casts on lvalues glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-07 22:18 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 18:27 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-07 14:14 -0500
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 20:16 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-07 15:41 -0500
Re: Casts on lvalues Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 03:03 -0800
Re: Casts on lvalues Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-09 17:54 +0200
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-09 12:38 -0500
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-09 18:50 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-09 14:31 -0500
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-09 20:12 -0800
Re: Casts on lvalues Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-12-10 07:38 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-10 08:12 -0500
Re: Casts on lvalues Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-12-10 17:46 +0000
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-10 12:59 -0500
Re: Casts on lvalues Keith Thompson <kst-u@mib.org> - 2012-12-10 12:07 -0800
Re: Casts on lvalues Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 03:11 -0800
Re: Casts on lvalues Ian Collins <ian-news@hotmail.com> - 2012-12-10 20:50 +1300
Re: Casts on lvalues James Kuyper <jameskuyper@verizon.net> - 2012-12-10 07:49 -0500
Re: Casts on lvalues Rui Maciel <rui.maciel@gmail.com> - 2012-12-07 15:19 +0000
Re: Casts on lvalues "BartC" <bc@freeuk.com> - 2012-12-07 16:54 +0000
csiph-web