3 ms·
Correct me if I'm wrong, but casting the result of malloc is generally considered bad form in C, isn't it?
by j_baker 15y ago
Correct me if I'm wrong, but casting the result of malloc is generally considered bad form in C, isn't it?
- jrockway 15y agoI'm just assuming it's a Windowsism.
- to3m 15y agoIn 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.
- nknight 15y agoI 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.
- mappu 15y agoI 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.
- unwind 15y agohttp://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc 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.