Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Bad things about C++

Name: Anonymous 2007-03-02 3:10 ID:8GT6+4Q4

Fuck OO. GOTO or GTFO.

Name: Anonymous 2009-01-18 10:39

>>29
You don't understand why you shouldn't cast malloc's value, and further, your statement suggests that you haven't done much work on systems other than 32-bit x86. Not that I'm suggesting that is the case; you may very well have written heaps of DEC Alpha code while blindfolded, and typing with your toes with your keyboard behind your back. Who knows.

First off, suppose, for whatever reason, you have carelessly omitted that #include <stdlib.h> that you ought to be writing whenever you use malloc. On x86, that's no big deal, because return type in C defaults to int. On any system, if you're casting your return, you won't even get warnings about it ... unless you have strict warnings enabled to catch the missing prototype, in which case if you're still casting the return value, you're basically just a robot doing stuff because that's what people have told you, and clearly have no idea what the fuck is going on. But that aside, int is four bytes, void * is four bytes, int * is four bytes, blah blah blah who cares, right? Until, of course, you build your code on a 64-bit system where sizeof(int) == 4 and sizeof(void *) is 8. Now your code is fucked, because you're casting things to the wrong-sized data.

The other, more significant reason is, any time you do an explicit cast, you're telling the compiler "I'M SMARTER THAN YOU, SHUT UP". If you're assigning from one type to another compatible type, a cast is completely unnecessary, and only will serve to cause problems later on when you decide that, damn, making all of those variables char was a dumbass idea, I really should've been using int all this time -- and suddenly your oh-so-clever casts are wiping out the high bits in all your data, and you're fired for being a nincompoop.

Aside from all this, the point ought to be irrelevant anyway, because in C++ you shouldn't even be using malloc in the first place. That's why it has that new operator that ensures type safety and all that other bullshit that makes you feel good about how awesome your code is.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List