Topic: regarding addition of an example to C Compatiblity section on explicit casts
Author: "James Kanze" <james.kanze@gmail.com>
Date: Thu, 22 Feb 2007 08:58:19 CST Raw View
Ganesh wrote:
> Consider this code segment:
> void * var = 0;
> short s = (short)(var);
> Given the size of short is less than the pointer type (which is the
> case in many of the widely used platforms), this code might be flagged
> as error by a C++ compiler, while a C compiler will silently accept it
> (as an explicit cast is present).
The conversion is "illegal" in both languages: undefined
behavior in C, but requiring a diagnostic in C++. (A diagnostic
is allowed in C, of course.)
> Now, my question is about listing this in the Annexure C- Compatiblity
> section. Currently, I see the mention of usage of casts in C.1.2 with
> two examples for 4.10 ("Converting void* to a pointer-to-object type
> requires casting") and ("Only pointers to non-const and non-volatile
> objects may be implicitly converted to void*"). Shouldn't this example
> be added to the list (which is about "explicit cast of void * to a
> type of shorter integral size")?
Strictly speaking, it's not really an incompatibility, since
both standards consider it illegal. In practice, of course,
most C compilers do give it a more or less defined behavior; in
particular, most C compilers guarantee a value preserving round
trip short->pointer->short.
In some ways, I feel that this should probably be defined by the
standard: the results of converting a pointer to a smaller
integral type are defined if the pointer was originally created
by conversion of the same or a smaller integral types. This
corresponds in fact to existing practice. It's not a big thing,
however, and I certainly wouldn't like to have to formulate the
actual standard text---the rules concerning null pointer
constants make it a bit awkward.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Martin Bonner" <martinfrompi@yahoo.co.uk>
Date: Fri, 23 Feb 2007 17:08:06 CST Raw View
On Feb 22, 2:58 pm, "James Kanze" <james.ka...@gmail.com> wrote:
> In some ways, I feel that this should probably be defined by the
> standard: the results of converting a pointer to a smaller
> integral type are defined if the pointer was originally created
> by conversion of the same or a smaller integral types. This
> corresponds in fact to existing practice.
I don't think this is a good idea. There have been architectures
where this sort of thing would not work.
For those who care, it's my favourite counter-example to most
assumptions about undefined behaviour - the Prime. Pointers had 16
bits of word offset, 12 bits of segment number, and a two-bit ring
field (higher numeric value was lower privilege). Any time a pointer
value was loaded into a pointer register, the ring bits in the
register were set to the OR of the current ring (which was usually
ring 3) and the bits in the value. Thus loading 0200000000 into a
pointer register and saving it, would save 1600000000 into memory.
Now obviously, a short would have been 16 bits so would be fine, but a
long would get corrupted (I found this out the hard way when I tried
something very similar to this sort thing).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Ganesh" <sgganesh@gmail.com>
Date: Wed, 21 Feb 2007 11:35:46 CST Raw View
Consider this code segment:
void * var = 0;
short s = (short)(var);
Given the size of short is less than the pointer type (which is the
case in many of the widely used platforms), this code might be flagged
as error by a C++ compiler, while a C compiler will silently accept it
(as an explicit cast is present).
Now, my question is about listing this in the Annexure C- Compatiblity
section. Currently, I see the mention of usage of casts in C.1.2 with
two examples for 4.10 ("Converting void* to a pointer-to-object type
requires casting") and ("Only pointers to non-const and non-volatile
objects may be implicitly converted to void*"). Shouldn't this example
be added to the list (which is about "explicit cast of void * to a
type of shorter integral size")?
Thanks!
-Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]