Small C "Puzzle"

N

Nudge

Minti said:
Why not cast? Are you saying this in context with the IA-64
thingy or just as a general thingy? I probably have never
understood the need to cast or not to cast. I seem to consider it
to be more of a matter of taste rather than anything else now. I
have seen lot of debate, Bjarne says use cast, everyone here says
no.

You need to tell your friend Bjarne to use a different language.
C++ perhaps?

^_^
 
M

Minti

Keith Thompson said:

<snip>

In answer to your question, it is never necessary to cast the result
of malloc() in C; it only masks errors. This is from the latest
version of the C FAQ (the web version hasn't yet been updated):

7.7: Why does some code carefully cast the values returned by malloc
to the pointer type being allocated?

A: Before ANSI/ISO Standard C introduced the void * generic pointer
type, these casts were typically required to silence warnings
(and perhaps induce conversions) when assigning between
incompatible pointer types.

Under ANSI/ISO Standard C, these casts are no longer necessary,
and in fact modern practice discourages them, since they can
camouflage important warnings which would otherwise be generated
if malloc() happened not to be declared correctly; see question
7.6 above. (However, the casts are typically seen in C code
which for one reason or another is intended to be compatible
with C++, where explicit casts from void * are required.)

References: H&S Sec. 16.1 pp. 386-7.


Well the web version at http://www.eskimo.com/~scs/C-faq/q7.7.html is just
an abridged version of this answer. But pardon me for being an ass-hole. But
after reading 7.6 from http://www.eskimo.com/~scs/C-faq/q7.6.html, I just
think that as long as <stdlib.h> is included there is NO need to have cast.
And if I am doing it I am neither hurting myself nor benefiting myself.
Except for the fact that if it is to be used with a C++ compiler { possibly
later } I won't have to "recast" my code.
 
K

Keith Thompson

Minti said:
Thanks, my mail program is configured to send both mail and post it to the
newsgroup, however as a I later checked up after disconnecting that it had
failed to upload the message. Ooops.

Please reconfigure your mail program so it *doesn't* send your both
messages both by e-mail and to the newsgroup.

More precisely, it's your mail program, so you can configure it any
way you like, but please don't send *me* e-mail messages that
duplicate what you post to Usenet. I'm sure that most other people
here feel the same way (I've seen plenty of complaints about the
practice). My complaint wasn't that your message didn't show up on
the newsgroup, it was that it did show up in my mailbox.

And please seriously consider what I wrote above about kissmyass.com.
 
K

Keith Thompson

Minti said:
Well the web version at http://www.eskimo.com/~scs/C-faq/q7.7.html is just
an abridged version of this answer.

Actually, the version I quoted is a newer (and better) version than
the one on the web.
But after reading 7.6 from
http://www.eskimo.com/~scs/C-faq/q7.6.html, I just think that as
long as <stdlib.h> is included there is NO need to have cast.
Absolutely.

And
if I am doing it I am neither hurting myself nor benefiting myself.

You're potentially masking the error of leaving out the "#include
Except for the fact that if it is to be used with a C++ compiler {
possibly later } I won't have to "recast" my code.

Why would you want to compile C code with a C++ compiler? They're two
different languages. (P.J. Plauger has explained is valid reasons for
doing this; very few other people should need to worry about it.)

If you want to write C++, write C++ (which means using new/delete
rather than malloc()/free().)
 
K

Keith Thompson

Keith Thompson said:
Why would you want to compile C code with a C++ compiler? They're two
different languages. (P.J. Plauger has explained is valid reasons for
doing this; very few other people should need to worry about it.)

Sorry, that was a typo; I meant to say that P.J. Plauger has explained
*his* valid reasons for compiling C code with a C++ compiler.
 
R

Randy Howard

AFAIK, IA stands for Intel Architecture.

Thus IA-32 is Intel's 32-bit ISA, i.e. x86, and IA-64 is Intel's
64-bit ISA, i.e. the Itanium Processor Family ISA.

And just to make it more confusing, Intel has started offering 64-bit
CPUs which effectively are clones of the AMD Opteron (apart from a
single instruction IIRC) which are collectively referred to as x86-64.
They (Intel) keep changing the marketing name, I think the latest
incantation is "EM64t".

Itanium != Opteron by any stretch of the imagination, although they
are both 64-bit processors.
 
W

Wei Li

You must havn't included the "malloc.h". At 64bit , the return value of a
function is defaulted to "int" if the declaration is not present.

Thanks!
Wei
 
F

Flash Gordon

You must havn't included the "malloc.h". At 64bit , the return value
of a function is defaulted to "int" if the declaration is not present.

<snip>

Please don't top-post. Replies belong under the text you are replying to
and what is not needed for context should be snipped.

malloc.h is a non-standard header. The header that provides a prototype
for malloc is stdlib.h
 
C

CBFalconer

Wei said:
You must havn't included the "malloc.h". At 64bit , the return
value of a function is defaulted to "int" if the declaration is
not present.

malloc.h is not a standard include file. stdlib.h is
appropriate. As others have pointed out, the problem is the lack
of the include together with the cast, which suppresses the
compiler warning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top