Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Pointers as error values |
| Date | 2015-10-19 11:53 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <n033ie$592$1@dont-email.me> (permalink) |
| References | <bb26d1c2-246b-45cd-8434-583530881c88@googlegroups.com> <f9691975-4af2-4bd4-a1eb-aa916cdf38ee@googlegroups.com> |
On 10/19/2015 11:02 AM, gwowen wrote: > On Monday, October 19, 2015 at 8:54:26 AM UTC+1, Morten W. Petersen wrote: >> Hi. >> >> I was thinking the other day, that NULL pointers indicate an error. That got me >> wondering, is it possible to use a low number range as valid error indicators? >> >> That is, could for example numbers from 1 to 7 be safely used as error indicators >> from a function that should return a pointer? > > In general, no. I'm sure Keith will quote you the exact sentence that disallows it. I haven't seen a response from Keith yet, so I'll handle it. The key problem is that numbers from 1 to 7 are not pointer values. They can be converted to pointer values, for example by using (void*)7. However, here's what the standard says about such conversions: "Except as previously specified," (and none of the previous specifications that it's referring to apply in this case) "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.3p5) First point: "implementation-defined". That allows, among other possibilities, that (void*)1 and (void*)7 might produce the same result. In particular, they might be null pointers. "not .. correctly aligned" is not a problem, as long as you're converting to void* or a pointer to a character type, which are not allowed to have alignment restrictions. If you're using T*, where _Alignof(T) > 1, then incorrect alignment is a possibility, leading to undefined behavior. Second point: "might not point to an entity of the referenced type". The results of comparing pointers for equality are defined for null pointers (which only compare equal to other null pointers); for all other pointers, whether or not two pointers compare equal depends upon whether they point at the same thing. (6.5.9p6) Because there's no guarantee that they point at any object, an implementation would be within it's rights to implement pointer equality comparisons in a way that causes (void*)1 to compare unequal to itself. Third point: "a trap representation" means that it might not be a valid pointer. Attempting to write such a pointer value to a pointer object has undefined behavior, as does attempting to read such a value from a pointer object. (6.2.5p5) Fourth point: you could avoid some of the above problems if you could convert the pointer back to the original integer value before performing such comparisons. However, here is everything that the standard says about such a conversion: "Except as previously specified," (and once again, none of the previous specifications is applicable in this case) "the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type." (6.3.2.3p6) Note, in particular, that (int)(void*)7 is not guaranteed to be 7. The typedefs [u]intptr_t are optional, but if they are supported by an implementation, and if p is a pointer to void, then it is guaranteed that (void*)(uintptr_t)p == p (7.20.1.4). However, there's no pointer type for which the inverse operation is guaranteed to be reversible. What you can do is define a fixed set of objects, one corresponding to each error code, and return the address of the corresponding object to indicate that the corresponding error has occurred. As long as you're defining the objects, it might make sense to store some useful information in them. -- James Kuyper
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-19 00:53 -0700
Re: Pointers as error values gazelle@shell.xmission.com (Kenny McCormack) - 2015-10-19 08:26 +0000
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-19 02:27 -0700
Re: Pointers as error values Richard Damon <Richard@Damon-Family.org> - 2015-10-19 23:38 -0400
Re: Pointers as error values jacobnavia <jacob@jacob.remcomp.fr> - 2015-10-19 10:34 +0200
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-19 02:29 -0700
Re: Pointers as error values mark.bluemel@gmail.com - 2015-10-19 05:53 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-19 10:00 +0000
Re: Pointers as error values Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-19 14:00 +0000
Re: Pointers as error values gwowen <gwowen@gmail.com> - 2015-10-19 08:02 -0700
Re: Pointers as error values James Kuyper <jameskuyper@verizon.net> - 2015-10-19 11:53 -0400
Re: Pointers as error values Ken Brody <kenbrody@spamcop.net> - 2015-10-19 11:25 -0400
Re: Pointers as error values supercat@casperkitty.com - 2015-10-19 08:41 -0700
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-19 08:59 -0700
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-21 07:32 +0200
Re: Pointers as error values Ian Collins <ian-news@hotmail.com> - 2015-10-21 19:17 +1300
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-21 08:31 +0200
Re: Pointers as error values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-21 12:46 +0100
Re: Pointers as error values Robert Wessel <robertwessel2@yahoo.com> - 2015-10-21 09:35 -0500
Re: Pointers as error values Ken Brody <kenbrody@spamcop.net> - 2015-10-21 15:24 -0400
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 05:44 +0000
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-21 10:41 -0700
Re: Pointers as error values supercat@casperkitty.com - 2015-10-21 11:28 -0700
Re: Pointers as error values John Temples <usenet@xargs-spam.com> - 2015-10-21 17:45 +0000
Re: Pointers as error values James Kuyper <jameskuyper@verizon.net> - 2015-10-21 14:15 -0400
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-29 16:59 +0100
Re: Pointers as error values supercat@casperkitty.com - 2015-10-29 12:35 -0700
Re: Pointers as error values raltbos@xs4all.nl (Richard Bos) - 2015-10-29 21:45 +0000
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-30 06:09 +0100
Re: Pointers as error values Rosario19 <Ros@invalid.invalid> - 2015-10-30 08:59 +0100
Re: Pointers as error values Rosario19 <Ros@invalid.invalid> - 2015-10-30 09:07 +0100
Re: Pointers as error values Rosario19 <Ros@invalid.invalid> - 2015-10-30 09:38 +0100
Re: Pointers as error values Rosario19 <Ros@invalid.invalid> - 2015-10-30 09:40 +0100
Re: Pointers as error values Robert Wessel <robertwessel2@yahoo.com> - 2015-10-30 04:56 -0500
Re: Pointers as error values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-30 11:00 +0000
Re: Pointers as error values Robert Wessel <robertwessel2@yahoo.com> - 2015-10-29 23:48 -0500
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-30 11:05 +0100
Re: Pointers as error values Richard Damon <Richard@Damon-Family.org> - 2015-10-30 07:45 -0400
Re: Pointers as error values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-30 05:04 -0700
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-30 16:09 +0100
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-30 11:16 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-30 20:58 +0000
Re: Pointers as error values Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-30 17:12 -0400
Re: Pointers as error values supercat@casperkitty.com - 2015-10-30 14:54 -0700
Re: Pointers as error values Robert Wessel <robertwessel2@yahoo.com> - 2015-10-30 18:48 -0500
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-01 04:00 +0000
Re: Pointers as error values Robert Wessel <robertwessel2@yahoo.com> - 2015-11-01 01:24 -0500
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-31 20:35 +0100
Re: Pointers as error values supercat@casperkitty.com - 2015-10-31 13:33 -0700
Re: Pointers as error values Richard Damon <Richard@Damon-Family.org> - 2015-10-30 22:19 -0400
Re: Pointers as error values supercat@casperkitty.com - 2015-10-30 08:22 -0700
Re: Pointers as error values Geoff <geoff@invalid.invalid> - 2015-10-30 13:11 -0700
Re: Pointers as error values Geoff <geoff@invalid.invalid> - 2015-10-30 13:23 -0700
Re: Pointers as error values Geoff <geoff@invalid.invalid> - 2015-10-21 11:13 -0700
Re: Pointers as error values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-21 20:31 +0100
Re: Pointers as error values Geoff <geoff@invalid.invalid> - 2015-10-22 07:13 -0700
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-26 07:16 +0100
Re: Pointers as error values Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-26 09:37 +0000
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 05:38 +0000
Re: Pointers as error values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-22 13:51 +0100
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-22 08:29 -0700
Re: Pointers as error values supercat@casperkitty.com - 2015-10-22 09:21 -0700
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-22 10:52 -0700
Re: Pointers as error values "Morten W. Petersen" <morphex@gmail.com> - 2015-10-26 07:20 +0100
Re: Pointers as error values Keith Thompson <kst-u@mib.org> - 2015-10-26 08:09 -0700
Re: Pointers as error values supercat@casperkitty.com - 2015-10-26 08:40 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-26 20:02 +0000
Re: Pointers as error values supercat@casperkitty.com - 2015-10-26 13:56 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 21:58 +0000
Re: Pointers as error values James Kuyper <jameskuyper@verizon.net> - 2015-10-22 19:12 -0400
Re: Pointers as error values asetofsymbols@gmail.com - 2015-10-22 18:36 -0700
Re: Pointers as error values asetofsymbols@gmail.com - 2015-10-29 12:47 -0700
Re: Pointers as error values Lőrinczy Zsigmond <nospam@for.me> - 2015-10-20 17:06 +0200
Re: Pointers as error values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-20 19:45 +0100
Re: Pointers as error values supercat@casperkitty.com - 2015-10-20 14:19 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-21 05:28 +0000
Re: Pointers as error values supercat@casperkitty.com - 2015-10-21 05:48 -0700
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 05:52 +0000
Re: Pointers as error values Ken Brody <kenbrody@spamcop.net> - 2015-10-21 15:19 -0400
Re: Pointers as error values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-22 06:06 +0000
csiph-web