🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2513

Re: Annotations on K&R II

#2513
From: telnet user
Date: Fri, 05 Apr 1996 00:38
58 lines
2904 bytes
Konrad Schwarz (schwarz@mips.complang.tuwien.ac.at) wrote:

: My understanding is that a conversion to a (void *) followed by a conversion
: to the original pointer type is the identity mapping; in other words, the mapping
: a C compilers uses between (void *) and any other pointer type is bijective.
: I don't know what you mean with representation; note however that
: 	T1 *p;
: 	*(T2 **) &p == (T2 *) p
: is not necessarily true, even, I think, if T1 or T2 is void.

: This is the reason why casting the comparison function argument
: of qsort to a function of two void pointers even if the actual argument
: is a function of two pointers to non-void is non-conforming, since the
: compiler now has no place to insert the pointer-conversion code.

To be a bit more explicit on that point, since it's a bit subtle:

This isn't guaranteed to work (though it is how many people do it):

---
int compare(const int *x, const int *y) {
    return *x - *y;
}

...
/* This next line casts (int (*)(const int *, const int *)) to 
 * (int (*)(const void *, const void *))
 * which is _not_ the same as casting (void *) to (int *).
 * The compiler can't conceivably insert conversion code here, because the
 * function isn't being called, and there are no args to 'convert'.
 * qsort() can't do the conversion, because the original type has been
 * 'casted away' already.
 */
qsort(base, num, sizeof(int), (int (*)(const void *, const void *))compare);
---

As opposed to the following:

---
int compare (const void *px, const void *py) {
    int x = *(int*)px; /* these are (void*)->(int*) casts, which are */
    int y = *(int*)py; /* guaranteed to work.  If the pointers have */
	               /* different formats, the compiler _must_ insert */
		       /* conversion code here */
    return x - y;
}

...
/* Note no cast needed here, since compare is already of the right
type */
qsort(base, num, sizeof(int), compare);
---

---------------------------------------------------------------------------
Tim Hollebeek         | Disclaimer :=> Everything above is a true statement,
Electron Psychologist |                for sufficiently false values of true.
Princeton University  | email: tim@wfn-shop.princeton.edu
----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
Technical Details
Message-ID: <4k1q1p$59d@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4jq1eq$a3c@solutions.solon.com> <4jr73o$h87@solutions.solon.com> <4jv6o8$coj@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: tim@franck.Princeton.EDU (telnet user)
Newsgroups: comp.lang.c.moderated
Subject: Re: Annotations on K&R II
Date: 4 Apr 1996 18:38:17 -0600
Organization: Princeton University
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4k1q1p$59d@solutions.solon.com>
References: <4jq1eq$a3c@solutions.solon.com> <4jr73o$h87@solutions.solon.com> <4jv6o8$coj@solutions.solon.com>
Reply-To: tim@wagner.Princeton.EDU
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: TIN [version 1.2 PL2]