From: Marius Nita <marius@cs.pdx.edu>
To: linux-c-programming@vger.kernel.org
Subject: Re: [jason@txt.com: Re: Problem with "chars"]
Date: Wed, 17 Jul 2002 12:30:24 -0700 [thread overview]
Message-ID: <20020717123023.A2248@cs.pdx.edu> (raw)
In-Reply-To: <200207171755.g6HHtsF0004688@grinch.txt.com>; from jason@txt.com on Wed, Jul 17, 2002 at 10:55:52AM -0700
On Wed, Jul 17, 2002 at 10:55:52AM -0700, Jason P. Winters wrote:
>
> > On Wed, Jul 17, 2002 at 09:33:16AM -0500, xlp wrote:
> > > char *foo()
> > > {
> > > char *retbar;
> > > retbar = (char *)malloc(20);
> >
> > Although it's tend to be an aesthitic issue, it is a *good* habit to not
> > cast malloc().
>
> Huh? I mean, excuse me? Where do you find a reference to _not_ casting
> the output of malloc()? Malloc returns a (void *) reference, you -need-
> to cast it to the type you're assigning it as.
The other folks already answered this, but it's worth pointing out that W
Richard Stevens talks about this in Advanced Programming in the Unix
Environment, and points out that the return of malloc should NOT be casted,
and stdlib.h should be included to avoid warnings..
It seems that this issue has become a religious thing, tho I don't see why. It
is clear that the return of malloc need not (and must not--for correctness) be
casted.
Besides,
struct Blah *foo = malloc( sizeof(struct Blah) );
looks a lot more readable than
struct Blah *foo = (struct Blah *)malloc( sizeof(struct Blah) );
> If you don't cast it, you get a warning message:
>
> tst.c: In function `foo':
> tst.c:17: warning: assignment makes pointer from integer without a cast
>
>
> So, do we ignore the message from the compiler?
>
> Now, *That's* a bad habit to get into...
>
> Ciao!
>
> Jason
> --
> |UUCP jason@txt.com Who wills, Can.
> |VOICE (408) 243-3425 Who tries, Does.
> |LOCAL Hey, Jason! Who loves, Lives. o_.
> |Disclaimer: Not me! I didn't do *THAT!* <|
> |Local Disclaimer: I'm not here! A. McCaffrey 4
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2002-07-17 19:30 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-07-17 14:33 [jason@txt.com: Re: Problem with "chars"] xlp
2002-07-17 17:55 ` Elias Athanasopoulos
2002-07-17 17:55 ` Jason P. Winters
2002-07-17 18:07 ` Glynn Clements
2002-07-17 18:24 ` Jason P. Winters
2002-07-17 18:54 ` Glynn Clements
2002-07-17 18:16 ` Elias Athanasopoulos
2002-07-17 19:30 ` Marius Nita [this message]
2002-07-18 14:43 ` Earl R. Lapus
2002-07-24 19:56 ` Mike Castle
2002-07-24 21:05 ` Jason P. Winters
2002-07-24 22:38 ` Glynn Clements
-- strict thread matches above, loose matches on Subject: below --
2002-07-18 7:11 Alvarez Alberto-AALVARB1
2002-07-18 9:25 ` Elias Athanasopoulos
2002-07-18 9:18 ` Marius Nita
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020717123023.A2248@cs.pdx.edu \
--to=marius@cs.pdx.edu \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).