Article View: comp.lang.c.moderated
Article #2480Re: void* type
From: Lawrence Kirby
Date: Wed, 03 Apr 1996 13:11
Date: Wed, 03 Apr 1996 13:11
42 lines
2097 bytes
2097 bytes
In article <4jq1e4$a2g@solutions.solon.com>
David_McReynolds@vos.stratus.com "David McReynolds" writes:
>Perhaps I'm confused but when I read the question this is what I came
>up with. It compiles and worked as expected in a test case.
>
>As far as the two pointers to be swapped are concerned, they are of
>the same type correct?
>
>I do not see the problem with:
>
>void swap_ptr(void **p1, void **p2) {
> void *tmp = *p1;
>
> *p1 = *p2;
> *p2 = tmp;
>
> return;
>}
This swaps 2 void * pointers, it isn't guaranteed to swap any other type
of pointer (except possibly char *). There is no guarantee that any other
pointer has the same size or representation as void * (except char *).
>Granted I did not do an extensive test but I did try ints, chars and floats
>with no surprises. As in:
You're assuming that all non-function pointers at least have the same size if
not the same representation. That may well hold on your platform but isn't
true in general. As far as the C language is concerned your code invokes
undefined behaviour and is not portable.
>Could someone enlighten me as to why this thread is so long?
Well, a couple of the articles suggest systems where your assumption
doesn't hold.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID:
<4jtteu$3lc@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jq1e4$a2g@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Lawrence Kirby <fred@genesis.demon.co.uk> Newsgroups: comp.lang.c.moderated Subject: Re: void* type Date: 3 Apr 1996 07:11:58 -0600 Organization: none Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jtteu$3lc@solutions.solon.com> References: <4iepre$ag7@solutions.solon.com> <4ii34g$nlp@solutions.solon.com> <4ja3qf$p9b@solutions.solon.com> <4jq1e4$a2g@solutions.solon.com> Reply-To: fred@genesis.demon.co.uk NNTP-Posting-Host: solutions.solon.com X-NNTP-Posting-Host: genesis.demon.co.uk X-Newsreader: Demon Internet Simple News v1.27 X-Mail2News-Path: genesis.demon.co.uk