Hacker News new | ask | show | jobs
by j_baker 5304 days ago
Correct me if I'm wrong, but casting the result of malloc is generally considered bad form in C, isn't it?
4 comments

In general, yes. For Win32, it is safe.

I would imagine they have activated "Compile as C++" for certain files. VC++ doesn't support C99, and it's probably easier to fix up any C++ incompatibilities than try to C89-ize everything.

http://stackoverflow.com/questions/605845/do-i-cast-the-resu... is a good place to start for a deeper discussion of this topic, if someone is interested.

My answer (obviously) is "yes", although as some folks point out here too, there are portability concerns that make it worth it sometimes.

I'm just assuming it's a Windowsism.
I wouldn't so much call it "bad form" as just non-idiomatic. It doesn't really have big downsides in practice.

It's mostly a habit people pick up from C++ (where it's mandatory due to stricter typing), and if you want to build C code with a C++ compiler, you need to add the casts.

Given Microsoft's C++ fetish, I'm unsurprised by this.

I suppose one downside would be if you cast a voidptr to a pointer to (an element that has a different size), then calling operator++ moves it by more than if you had left it as a voidptr.