Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is the value after operator '!'? |
| Date | 2015-10-26 15:15 -0700 |
| Organization | None to speak of |
| Message-ID | <lnvb9te0o4.fsf@kst-u.example.com> (permalink) |
| References | (1 earlier) <87k2q9lp98.fsf@bsb.me.uk> <090eee37-7aeb-47e7-b195-ce3900b30fff@googlegroups.com> <87si4xjzui.fsf@bsb.me.uk> <ln8u6pfqup.fsf@kst-u.example.com> <d0132578-984d-47b0-995e-a3d2aebeeaaf@googlegroups.com> |
supercat@casperkitty.com writes:
> On Monday, October 26, 2015 at 1:05:12 PM UTC-5, Keith Thompson wrote:
>> There is. The expression 0 is of type int. In p==0, if p is of pointer
>> type, the expression 0 is converted, via the special-case rule in N1570
>> 6.3.2.3p3, from int to the type of p. (This conversion is almost
>> certainly done at compile time, though the standard doesn't say so.)
>
> In a system where (char*)x would yield a pointer to physical address x,
> and where physical address zero was valid, but where (char*)0 [i.e. NULL]
> was represented by a pointer with some physical address other than zero,
> would there be any way of writing a pointer constant equal to physical
> address zero?
No portable way -- but then pointer constants that refer to numeric
addresses are inherently non-portable anyway. A compiler could provide
some special-case way of representing address zero if it were useful.
>> > As for the very last part, I don't think so. Both p and q are null
>> > pointers of the same type. Whilst I agree that !(intptr_t)p might be
>> > 0, I can't see how can !q be anything other than 1.
>>
>> Agreed. (1-1) is a null pointer constant, and so is (intprt_t)(1-1)
>> (since casts are permitted in integer constant expressions). An integer
>> constant expression with the value 0 cast to any pointer type is a null
>> pointer constant, so (unsigned char*)(intptr_t)(1-1) is a null pointer
>> constant.
>
> I guess "1-1" wasn't sufficiently subtle for a compiler to miss that it was
> really a constant; C# distinguishes between literal zeroes and integer
> constants whose value happens to equal zero, but I guess C doesn't?
Why guess? N1570 6.3.2.3 defines null pointer constants. 6.6 defines
integer constant expressions. Read The Fine Manual.
> Would the value of (int)0.3 be considered an integer constant? What about
> (int)(1.0E+17+1.0-1.0E+17), whose value would depend upon how floating-point
> precision was handled?
N1570 6.6. Yes, it's possible to write an integer constant expression
whose value is implementation-defined. (CHAR_BIT is another example of
such an expression.)
> I sometimes wish the C Standard would define an intrinsic which would take
> a possibly-constant argument and yield a value equal to the argument but
> behave as though the value wasn't known until run-time since, especially in
> the embedded-systems world, it may be necessary to have various things take
> the same amount of time regardless of whether a particular argument is or
> is not a constant.
A separately compiled function that returns its argument might be
sufficient for some purposes.
>
>> A better example would be:
>>
>> unsigned char *p = 0;
>> const int not_a_constant zero = 0;
>> unsigned char *q = (unsigned char*)zero;
>>
>> The value of p is a null pointer. The value of q may or may not be.
>>
>> And converting a null pointer value to intptr_t may or may not yield 0.
>
> Are there any other cases in C where an expression which the compiler can
> recognize as being equal to zero, and an expression always happens to yield
> zero but isn't recognized as such, would both be valid but semantically
> different?
A compiler isn't required to recognize that the value of `zero` given
const int zero = 0;
is always 0, but I'd be surprised by a compiler that didn't do so.
> I wonder how much code would break if the Standard were to
> require that the argument to the cast must have one of a small number of
> literal forms (e.g. 0, 0L, etc.) rather than merely being any constant
> expression whose value happens to be zero?
I wonder how useful such a change would really be. If you want a
pointer value whose representation is all-bits-zero, define a pointer
object and call memset(). But I've never felt the need for such a
pointer value.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What is the value after operator '!'? fl <rxjwg98@gmail.com> - 2015-10-26 06:21 -0700
Re: What is the value after operator '!'? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-26 09:29 -0400
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 13:42 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 07:53 -0700
Re: What is the value after operator '!'? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 17:37 +0000
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 11:05 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 11:38 -0700
Re: What is the value after operator '!'? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-26 20:27 +0000
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 14:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:15 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 17:12 -0700
Re: What is the value after operator '!'? Ian Collins <ian-news@hotmail.com> - 2015-10-27 10:27 +1300
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-26 15:15 -0700
Re: What is the value after operator '!'? supercat@casperkitty.com - 2015-10-26 16:16 -0700
Re: What is the value after operator '!'? Keith Thompson <kst-u@mib.org> - 2015-10-27 08:39 -0700
Re: What is the value after operator '!'? Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 14:04 +0000
Re: What is the value after operator '!'? Geoff <geoff@invalid.invalid> - 2015-10-26 09:50 -0700
csiph-web