TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: you
from: Jerry Coffin
date: 2003-09-12 19:07:00
subject: Re: Re: Pointers

From: jcoffin{at}taeus.com
To: c_echo{at}yahoogroups.com

At 11:06 AM 9/12/2003 +0100, you wrote:

[ ... ]

>I was taught that, under the threat of death, the return from malloc()
>_must always_ be cast from VOID *.  Is that no longer true?  What has
>changed?

Nothing really -- back in the far pre-ANSI days, when malloc returned a
char *, you had no choice but to cast its result when assigning to anything
other than pointer to char.

When it started to return a pointer to void, (not VOID) that changed: the
cast is no longer required, and is not desirable.  Specifically, if you
forget to include , without the cast, you'll get at least a
warning telling you that you're converting from the default type (int) to
whatever type of pointer you're assigning to.

If you include the cast, you won't get a message, because the cast
basically tells the compiler "shut up -- I know what I'm doing".  The bug
involved is covered up as long as you're in the (fairly common) situation
that the pointer it returns can be represented as an int without losing
anything.  OTOH, if (for example) you do it under DOS in compact or large
model, it treats the returned pointers as a (16-bit) int, and then converts
that to a (32-bit) pointer by filling the high 16-bits with zeros,
overwriting the top 16-bits that were really supposed to be there.

The result is that you end up overwriting something you were never supposed
to touch in the first place.
         Later,
         Jerry.

--- SoupGate-Win32 v1.05
* Origin: jcoffin{at}taeus.com (2:292/516.666)
SEEN-BY: 633/267 270
@PATH: 292/516 854 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.