🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: pointer casting

#2615
From: J. Kanze
Date: Thu, 18 Apr 1996 12:31
32 lines
2232 bytes
In article <4l33mf$7pr@solutions.solon.com> msz@sirius.astrouw.edu.pl
(Michal Szymanski) writes:

|> There is a discussion of (void *) and pointer casting going on this list.
|> K&R "Ansi C" book says that conversion: (type *) -> (void *) -> (type *)
|> is guaranteed to yield the same pointer. This (and the discussion on the
|> list) suggests that another chain: (void *) -> (type *) -> (void *)
|> might result in some change of the original pointer. In other words
|> I would guess that casting a void pointer to a pointer to a type requiring
|> special memory allignment might involve adjusting it to a proper value.
|> Is this true? I played with such conversions on Sun Sparcstation
|> (which surely requires proper allignment of data with sizeof > 1),
|> using both Sun compiler v. 2.0.1 and Gnu 2.6.3, but never observed
|> such an automatic adjustment? Odd pointers casted to (e.g.) double
|> result in Bus Error when referenced. And they show the same odd value
|> when printed as long values.

|> So I wonder whether this adjustment is just (nowhere-implemented) possibility 
|> or some real thing?

The adjustment is certainly legal, although I don't know of an
implementation that does it.  More of a problem occurs, however, on
implementations where `type*' contains less bits than `void*'.
Obviously, in such cases, some information will be lost.  (Typically,
this will `look like' the compiler has adjusted the pointer downward.
In fact, it is just a case of throwing away the bits it doesn't have
room for.)
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
Technical Details
Message-ID: <4l5cmg$1ub@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4l33mf$7pr@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.lang.c.moderated
Subject: Re: pointer casting
Date: 18 Apr 1996 07:31:12 -0500
Organization: GABI Software, Sarl.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4l5cmg$1ub@solutions.solon.com>
References: <4l33mf$7pr@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com