Re: is c++ requirement to cast pointers(like the one returned from
malloc) redundant ?
On Mar 13, 10:22 pm, Ian Collins <ian-n...@hotmail.com> wrote:
On 03/14/10 11:22 AM, Rafael Anschau wrote:
[...]
> Thanks for the answers, I am aware of the "efficient c++"
> guidelines(new, vector etc), and no I don=B4t use malloc in
> C++. I was helping to debug someone else=B4s code when I
> realized the reason itdidn =B4t compile was that the person
> was saving .cpp. And that in C++, you need to cast malloc=B4s
> return. My doubt is if people agree/disagree that need for
> cast in malloc is redundant. Magnus, I agree that making
> it explicit forces you to check your assumptions, and that
> not checking could get you into problems. That maybe the
> reason for it(I looked up annotated c++ and didn=B4t find
> anything about it, but that=B4s a good justification).
The cast isn't redundant. I fact it was added to C++ (you
don't have to cast in C) when C's automatic conversion from
void* was removed.
Just a nit, but I think that the correct formulation in the last
phrase would be that "standard C's automatic conversion from
void* wasn't added to C". I don't have my copy of K&R1 handy,
but IIRC, pointers to different types didn't implicitly convert
in it (although---again, IIRC---there was a statement to the
effect that this wasn't enforced in current compilers).
C++ was originally developed before the C committee added void
and void* (the C committee adopted these from C), and it didn't
have the conversion. The conversion is an innovation of the C
committee, and not conform with the original intent in C. (It
may be supposed that Stroustrup was familiar with the original
intent in C when he invented void and void*, given that his boss
at the time was Brian Kernighan.)
--
James Kanze