Article View: comp.std.c
Article #3290Re: can a pointer cast to a void* be compared to (void*)0 ?
From: Norman Diamond
Date: Mon, 25 Jan 1993 01:52
Date: Mon, 25 Jan 1993 01:52
50 lines
3244 bytes
3244 bytes
In article <28570@dog.ee.lbl.gov> torek@horse.ee.lbl.gov (Chris Torek) writes: >In article <alien.02pi@acheron.amigans.gen.nz> alien@acheron.amigans.gen.nz >(Ross Smith) writes: >>standard does not require pointers to functions to be castable to/from any >>other pointer type. >Yes (or rather, the effect is undefined or unspecified or otherwise If a converted pointer is used to call a function that has a type that is not compatible with the type of the called function, the behavior is undefined. By intuitive implication, I'd say that if the converted pointer is used to call a function that has a type compatible with the converted pointer, it works. >>(1) Are pointers to different types of function required to be castable to >>each other? >Yes; however, the result is up to the implementation. Your only guaranteee >is that if the result is then cast *back* to the `correct' type [...] >the final result can be used to *call* that function; I am somewhat >less certain that it must compare equal to the original pointer. It must compare equal. >>(2) If a compiler does allow casting between "ordinary" pointers and function >>pointers, does the rule about null pointers remaining null still apply? >Note, however, that the null pointer constant [blah blah blah...] Doesn't look like he asked about that one though. If an implementation says that an object-or-incomplete pointer is allowed to be cast to a function pointer (or vice-versa), then see what the implementation says if the original pointer happens to be null. If the implementation doesn't say, and you've just been lucky so far in playing with undefined behavior, then it's up to you to decide if you want to continue depending on luck. >Another question (which belongs in comp.std.c; note followup header): >do null pointer constants, cast or assigned to pointers to functions >of different types, then to a common type, compare equal? If two pointers to function types are both null pointers or both point to the same function, they compare equal. But you can't use this to compare two incompatible function pointer types because of the constraints. So this reduces to the question, if a null function pointer is cast to a different type of function pointer, is the result still a null pointer? Oh good, yes. [ANSI classic 3.3.9 and last paragraph of 3.2.2.3.] -- Norman Diamond diamond@jit.dec.com If this were the company's opinion, I wouldn't be allowed to post it. Pardon me? Or do I have to commit a crime first?
Technical Details
Message-ID:
<1993Jan25.015214.22668@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> <alien.02pi@acheron.amigans.gen.nz> <28570@dog.ee.lbl.gov>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!olivea!decwrl!pa.dec.com!engage.pko.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Newsgroups: comp.std.c Subject: Re: can a pointer cast to a void* be compared to (void*)0 ? Message-ID: <1993Jan25.015214.22668@nntpd.lkg.dec.com> Date: 25 Jan 93 01:52:14 GMT References: <C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> <alien.02pi@acheron.amigans.gen.nz> <28570@dog.ee.lbl.gov> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo Lines: 50