Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Szabolcs Nagy <nsz@port70.net> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: null pointer constant, address constant |
| Date | 2012-12-18 20:06 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <kaqibr$gj3$1@speranza.aioe.org> (permalink) |
| References | <kapqt2$es4$1@speranza.aioe.org> <kaq01g$6jp$1@dont-email.me> |
James Kuyper <jameskuyper@verizon.net> wrote: > On 12/18/2012 08:25 AM, Szabolcs Nagy wrote: >> it seems the definition of 'null pointer constant' >> relies on integer constant expressions (6.3.2.3p3) >> http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p3 >> >> so eg. >> >> (void*)((char*)1 - 1) >> >> is not a null pointer constant because pointer >> arithmetics cannot give an integer constant expression >> >> but at least this could be used in pointer initializers >> since address constants are allowed there (6.6p7) > > Two things to keep in mind: > > When converting an arbitrary integer to a pointer type "the result is > implementation-defined, might not be correctly aligned, might not point > to an entity of the referenced type, and might be a trap > representation." (6.3.2.3p1) does not matter, your concerns only matter if the expression is evaluated, but it is not: sizeof( *(0?(double*)0:(void*)((char*)1 - 1)) ) is this equivalent to sizeof(double) or sizeof(void)? (now that the expression after : is not a null pointer constant we know the result is sizeof(void) and thus a constraint violation) you don't need to evaluate the expression to use the "constness" of it so any argument about trap representations and alignment is moot > However, I get the impression that you were expecting > > (char*)1-1 == (char*)0. > > The standard says nothing of the kind - in fact, if that expression has > defined behavior, it's guaranteed to be false. Here's why: if the > implementation does define (char*)1 as pointing at an element, other > than the first, of an array, then (char*)1 - 1 will point at the > immediately preceding element of the array, and therefore, points at an > object. Now (char*)0 is a null pointer, and a null pointer can never > compare equal to a pointer to an object (6.3.2.3p3). Therefore, if the this is a valid argument > I may be missing something, but as far as I can tell, the rules given in > 6.5.15p6 give the same result whether the second or third operand is a > null pointer constant, or merely a constant expression with the type > void* and a null value. Could you provide a counter-example? yes (0 ? (double*)0 : (void*)0) has type double*, while (0 ? (double*)0 : (void*)1) has type void* to exploit this difference you can use sizeof as above >> but (for me at least) it's more surprising that this is >> not even a constant expression that can be used in >> initializers, ie. >> >> void *p = 0?(void*)0:(void*)0; >> >> is undefined behaviour if i read section 6.6 correctly >> (not even a constraint violation that requires a diagnostic) > > That does seem a bit surprising, but I believe you're correct. Note, ok i was wrong about this, an address constant can be a null pointer (not just null pointer constant) > This imposes a bit of a strain on the implementor, who would otherwise > be allowed to delay evaluation of such expressions until run-time. The > rules for integer constant expressions were intended to keep them > simple, to make them easy to identify and evaluate. my problem is that they are not easy to identify !(int)1.0 (void*)(1-1) are integer constant expressions and null pointer constants (int)!1.0 (void*)(char*)0 (char*)(void*)0 (void*)(void*)0 (void*)((char*)0-(char*)0) are neither
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 13:25 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 09:53 -0500
Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 20:06 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 15:39 -0500
Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-19 00:44 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 20:24 -0500
Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-19 10:27 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-19 07:15 -0500
Re: null pointer constant, address constant Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-21 03:03 +0200
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-21 12:13 -0500
Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2013-01-09 00:44 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2013-01-08 20:21 -0500
Re: null pointer constant, address constant Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-08 20:46 -0500
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 20:37 -0500
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 16:15 -0500
Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-18 15:52 -0800
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 19:12 -0500
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 19:51 -0500
Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-18 17:02 -0800
Re: null pointer constant, address constant "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-12-20 05:34 -0800
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-20 10:33 -0500
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-20 12:28 -0500
Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:13 -0800
Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:05 -0800
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-20 12:17 -0500
Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-20 12:10 -0800
Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:25 -0800
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 14:02 -0500
Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 20:09 +0000
Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 15:54 -0500
Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 16:18 -0500
csiph-web