Topic: null pointer constants


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/10/24
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

>In article <3630ACF9.72FF721A@sensor.com>, Ron Natalie <ron@sensor.com>
>writes

>>Francis Glassborow wrote:

>>> but does it in C++ where i is a compile time constant?  Of course the
>>> answer is somewhere in the C++ standard but perhaps someone already
>>> knows where.

>>Yes, it is in the standard.  5.19 defines a constant integer
>>expression to include const variables.  4.10 uses that term
>>directly to define the null pointer constant.

>Thanks.  I do not remember seeing this listed as and incompatibility
>with C.

What incompatibility?

C++ null pointer constant, 4.10:
"A null pointer constant is an integral constant expression (5.19)
rvalue of integer type that evaluates to zero."

C null pointer constant, 6.2.2.3:
"An integral constant expression with the value 0, or such an
expression cast to void*, is called a null pointer constant."

(I don't think we are talking about the void* difference.)

C++ integral constant-expression, 5.19:
"An integral constant-expression can involve only literals (2.13),
enumerators, const variables or static data members of integral or
enumeration types initialized with constant expressions (8.5),
non-type template parameters of integral or enumeration types,
and sizeof expressions.  Floating literals (2.13.3) can appear only
if they are cast to integral or enumeration types."

C integral constant-expressions, 6.4:
"An integral constant expression shall have integral type and shall
only have operands that are integer constants, enumeration constants,
character constants, sizeof expressions, and floating constants
that are the immediate operands of casts."

Apart from the void* restriction, what C null pointer constant is
not also a C++ null pointer constant?

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/10/24
Raw View
In article <$5aKilADv6L2Ew88@robinton.demon.co.uk>,
  Francis Glassborow <francisG@robinton.demon.co.uk> wrote:
> A small question that crossed my mind while reading a thread in
> comp.std.c

> const int i = 0;
> char * ptr = (char *)i;

> does not necessarily make ptr a null pointer in C (i is not a null
> pointer constant)

> but does it in C++ where i is a compile time constant?

Yes, absolutely yes.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/24
Raw View
In article <70qqpm$mib$1@engnews1.eng.sun.com>, Steve Clamage
<clamage@Eng.Sun.COM> writes
>Apart from the void* restriction, what C null pointer constant is
>not also a C++ null pointer constant?

int const zero = 0;

int main(){
        char * cptr = (char *) zero;
        return 0;
}

Now if I understand the rules correctly, cptr is required to be a null
pointer in C++ and is required to be a pointer that results from
treating the value of zero as a pointer.  While these are usually the
same thing that is by no means always the case.  C seems to be clear
that the zero above is NOT a null pointer constant.

The critical difference is that zero in the above is an integer constant
in C++ but is not in C.


Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/25
Raw View
Steve Clamage wrote:

> "An integral constant expression shall have integral type and shall
> only have operands that are integer constants, enumeration constants,
> character constants, sizeof expressions, and floating constants
> that are the immediate operands of casts."

Note that it doesn't say anything about const variables here.

const int x = 0;


 x is a constant expression in C++, but not in C.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1998/10/25
Raw View
Francis Glassborow <francisG@robinton.demon.co.uk> wrote:

> const int i = 0;
> char * ptr = (char *)i;

[ Not null in C but maybe null in C++ ]

> I do not remember seeing this listed as an incompatibility with C.

clamage@Eng.Sun.COM (Steve Clamage) wrote:


: What incompatibility?

: C++ integral constant-expression, 5.19:
...
: enumerators, const variables or static data members of integral or
---------------^^^^^^^^^^^^^^^
...

: C integral constant-expressions, 6.4:
: "An integral constant expression shall have integral type and shall
: only have operands that are integer constants, enumeration constants,
: character constants, sizeof expressions, and floating constants
: that are the immediate operands of casts."

I don't see those words here.  Maybe I missed something.

John



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/10/25
Raw View
Ron Natalie <ron@sensor.com> writes:

>Steve Clamage wrote:

>> "An integral constant expression shall have integral type and shall
>> only have operands that are integer constants, enumeration constants,
>> character constants, sizeof expressions, and floating constants
>> that are the immediate operands of casts."

>Note that it doesn't say anything about const variables here.

>const int x = 0;

> x is a constant expression in C++, but not in C.

Which I believe is what I said.

The question was about incompatibility of C null pointer constants
in C++. Every C null pointer constant of integral type is also
a C++ null pointer constant.

--
Steve Clamage, stephen.clamage@sun.com


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/10/25
Raw View
jpotter@falcon.lhup.edu (John Potter) writes:

>Francis Glassborow <francisG@robinton.demon.co.uk> wrote:

>> const int i = 0;
>> char * ptr = (char *)i;

>[ Not null in C but maybe null in C++ ]

>> I do not remember seeing this listed as an incompatibility with C.

>clamage@Eng.Sun.COM (Steve Clamage) wrote:

>: What incompatibility?

>: C++ integral constant-expression, 5.19:
>...
>: enumerators, const variables or static data members of integral or
>---------------^^^^^^^^^^^^^^^
>...

>: C integral constant-expressions, 6.4:
>: "An integral constant expression shall have integral type and shall
>: only have operands that are integer constants, enumeration constants,
>: character constants, sizeof expressions, and floating constants
>: that are the immediate operands of casts."

>I don't see those words here.  Maybe I missed something.

The question was about incompatibility of C null pointer constants
in C++. Every C null pointer constant of integral type is also
a C++ null pointer constant.

--
Steve Clamage, stephen.clamage@sun.com


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: 1998/10/26
Raw View
Steve Clamage wrote:
....
> The question was about incompatibility of C null pointer constants
> in C++. Every C null pointer constant of integral type is also
> a C++ null pointer constant.

But not vice-versa.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/26
Raw View
In article <70vo3o$2lo$1@engnews1.eng.sun.com>, Steve Clamage
<stephen.clamage@sun.com> writes
>The question was about incompatibility of C null pointer constants
>in C++. Every C null pointer constant of integral type is also
>a C++ null pointer constant.

I am quite happy when C++ makes something that was undefined in C
defined in C++, I'll even accept that C++ might provide 'implementation
defined' on the basis that it is in some way a kind of implementation of
C.  However when C and C++ provide different semantics for legal syntax
then we have an incompatibility.  No big deal in this case because
something that can be a non-null pointer in C must be one in C++ but not
vice versa.  Now if C++ defined a cast of a zero integral to a value to
a pointer to be a null pointer (i.e. accept that int z=0; char * nptr =
(char *) z; must make nptr a null pointer) I would be happy as it would
become documented 'implementation defined behavoiur'



Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/10/26
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

>In article <70qqpm$mib$1@engnews1.eng.sun.com>, Steve Clamage
><clamage@Eng.Sun.COM> writes
>>Apart from the void* restriction, what C null pointer constant is
>>not also a C++ null pointer constant?

>int const zero = 0;

>int main(){
>        char * cptr = (char *) zero;
>        return 0;
>}

The cast is needed in C, but not in C++. The code as written
is valid in both languages.

>Now if I understand the rules correctly, cptr is required to be a null
>pointer in C++ and is required to be a pointer that results from
>treating the value of zero as a pointer.  While these are usually the
>same thing that is by no means always the case.  C seems to be clear
>that the zero above is NOT a null pointer constant.

>The critical difference is that zero in the above is an integer constant
>in C++ but is not in C.

Yes, but your concern is backwards. (See my question quoted above.)

When we speak of C compatibility, we mean a construct in C that
has the same meaning when treated as C++. There are billions and
billions of C++ constructs that are not valid C code.

Trying to compile C++ code with a C compiler is an exercise
in futility.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1998/10/26
Raw View
In article <70u0i8$4fl$1@pigpen.csrlink.net>,
John Potter <jpotter@falcon.lhup.edu> wrote:
>
>Francis Glassborow <francisG@robinton.demon.co.uk> wrote:
>: What incompatibility?
>
>: C++ integral constant-expression, 5.19:
>...
>: enumerators, const variables or static data members of integral or
>---------------^^^^^^^^^^^^^^^
>...
>
>: C integral constant-expressions, 6.4:
>: "An integral constant expression shall have integral type and shall
>: only have operands that are integer constants, enumeration constants,
>: character constants, sizeof expressions, and floating constants
>: that are the immediate operands of casts."
>
>I don't see those words here.  Maybe I missed something.

It's a difference, but not an incompatibility, because any valid use of a
null pointer constant in C will also be a valid use of a null pointer
constant in C++.  It doesn't go the other way, but that's true for a large
part of C++.  So in this context, "incompatibility" is usually understood
to mean constructs that C has that don't work the same way in C++
(e.g. empty parameter lists in function declarations -- in C they mean
K&R-like semantics, in C++ they're equivalent to C's (void) parameter
list).

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/10/26
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

>In article <70vo3o$2lo$1@engnews1.eng.sun.com>, Steve Clamage
><stephen.clamage@sun.com> writes
>>The question was about incompatibility of C null pointer constants
>>in C++. Every C null pointer constant of integral type is also
>>a C++ null pointer constant.

>I am quite happy when C++ makes something that was undefined in C
>defined in C++, I'll even accept that C++ might provide 'implementation
>defined' on the basis that it is in some way a kind of implementation of
>C.  However when C and C++ provide different semantics for legal syntax
>then we have an incompatibility.  No big deal in this case because
>something that can be a non-null pointer in C must be one in C++ but not
>vice versa.

I'm not sure I get your point. The original question was about
something like
 const int j = 0;
 char * ptr = (char *)j;

In C, the results are implementation-defined, because the j
is not a null pointer constant.  Typically, ptr will be
a null pointer, but you can't depend on it.

In C++, the results are defined, and ptr is always a null pointer.

Is your concern about the case where ptr was not null but had some
predictable result in C on some system?  That's pretty subtle,
but I agree it is a change in meaning. But it's not the only case
of a valid construct having different defined semantics in C and C++,
and in my view is the least likely to cause a change in program
behavior.

>Now if C++ defined a cast of a zero integral to a value to
>a pointer to be a null pointer (i.e. accept that int z=0; char * nptr =
>(char *) z; must make nptr a null pointer) I would be happy as it would
>become documented 'implementation defined behavoiur'

Now you've really lost me. The results of your second example
are implementation-defined in both C and C++.  When behavior is
implementation-defined, an implementor of C and C++ could choose
the same behavior for both. If an existing C compiler provided
different semantics from C++, the implementor would be reluctant
to change the behavior of the C compiler to match C++.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/26
Raw View
Steve Clamage wrote:

>
> In C++, the results are defined, and ptr is always a null pointer.
>
Note that ptr is a null char pointer.  There is no such thing
as a generic null pointer, only the null pointer constant, and
null pointers of various pointer types.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/10/26
Raw View
Steve Clamage wrote:

 > Francis Glassborow <francis@robinton.demon.co.uk> writes:

 > >In article <70vo3o$2lo$1@engnews1.eng.sun.com>, Steve Clamage
 > ><stephen.clamage@sun.com> writes
 > >>The question was about incompatibility of C null pointer constants
 > >>in C++. Every C null pointer constant of integral type is also
 > >>a C++ null pointer constant.

 > >I am quite happy when C++ makes something that was undefined in C
 > >defined in C++, I'll even accept that C++ might provide 'implementation
 > >defined' on the basis that it is in some way a kind of implementation of
 > >C.  However when C and C++ provide different semantics for legal syntax
 > >then we have an incompatibility.  No big deal in this case because
 > >something that can be a non-null pointer in C must be one in C++ but not
 > >vice versa.

 > I'm not sure I get your point. The original question was about
 > something like
 >         const int j = 0;
 >         char * ptr = (char *)j;

 > In C, the results are implementation-defined, because the j
 > is not a null pointer constant.  Typically, ptr will be
 > a null pointer, but you can't depend on it.

 > In C++, the results are defined, and ptr is always a null pointer.

 > Is your concern about the case where ptr was not null but had some
 > predictable result in C on some system?  That's pretty subtle,
 > but I agree it is a change in meaning. But it's not the only case
 > of a valid construct having different defined semantics in C and C++,
 > and in my view is the least likely to cause a change in program
 > behavior.

And a great many of those incompatibilities are listed in Appendix C.
Why wasn't this one?



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/10/27
Raw View
In article 41C6@wizard.net, James Kuyper <kuyper@wizard.net> writes:

>Steve Clamage wrote:

> > Is your concern about the case where ptr was not null but had some
> > predictable result in C on some system?  That's pretty subtle,
> > but I agree it is a change in meaning. But it's not the only case
> > of a valid construct having different defined semantics in C and C++,
> > and in my view is the least likely to cause a change in program
> > behavior.

>And a great many of those incompatibilities are listed in Appendix C.
>Why wasn't this one?

I'm sure it's because nobody thought of it.

I'll submit this topic to the C++ committee for possible treatment
as a defect report. (There is as yet no mechanism in place for
non-committee-members to submit defect reports. When there is a
mechanism in place, information on it will be posted here and in
other places, including the comp.std.c++ FAQ.)

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jerry Leichter <leichter@smarts.com>
Date: 1998/10/27
Raw View
Considering the length of this thread, the depth and subtlety of some of
the points being made, and finally the difference between C and C++ on
an issue that, on the surface, seems as if it should be the same for
both ... does the repeated assertion that "No C/C++ don't need a
built-in NIL pointer, 0 *is* the null pointer" still sound convincing to
anyone?
       -- Jerry


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/23
Raw View
A small question that crossed my mind while reading a thread in
comp.std.c

const int i = 0;
char * ptr = (char *)i;

does not necessarily make ptr a null pointer in C (i is not a null
pointer constant)

but does it in C++ where i is a compile time constant?  Of course the
answer is somewhere in the C++ standard but perhaps someone already
knows where.


Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/23
Raw View
Francis Glassborow wrote:

>
> but does it in C++ where i is a compile time constant?  Of course the
> answer is somewhere in the C++ standard but perhaps someone already
> knows where.
>

Yes, it is in the standard.  5.19 defines a constant integer
expression to include const variables.  4.10 uses that term
directly to define the null pointer constant.



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/10/23
Raw View
In article <3630ACF9.72FF721A@sensor.com>, Ron Natalie <ron@sensor.com>
writes

>Francis Glassborow wrote:

>> but does it in C++ where i is a compile time constant?  Of course the
>> answer is somewhere in the C++ standard but perhaps someone already
>> knows where.

>Yes, it is in the standard.  5.19 defines a constant integer
>expression to include const variables.  4.10 uses that term
>directly to define the null pointer constant.

Thanks.  I do not remember seeing this listed as and incompatibility
with C.

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]