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

C vs C++ vs Something Else

Name: Anonymous 2010-04-07 17:09

Hi folks,

After getting frustrated that I couldn't easily accomplish what I wanted to in C, such as growable type-safe arrays and inheritance/polymorphism, I recently partially converted my ~10ksloc game project to C++. I didn't convert it all at once; I just made it all compile as C++, and gradually converted it as I worked on new stuff. About half of it is OOP/templates, and the other half is still pure C99 which compiles as C++. Notice I said 'easily'; I did write a type-safe vector template as a macro which you explicitly instantiate, and I did have a reasonably complex inheritance tree by writing vtables manually. These were just a pain in the ass to maintain.

Now I am somewhat regretting this decision. I'm realizing that these things that pushed me to convert it to C++, I shouldn't really have been doing them in the first place. Growable vectors (such as std::vector) are a terrible idea. Dynamic memory allocation is just a bad idea in general. You can't use these because an allocation might fail at any moment. Whether you throw an exception or you correctly handle and propagate a memory error via return values, it doesn't matter; you can't usefully recover in the middle of an operation. The only sane way is to pre-allocate everything you need at load time. If you do this properly, growable vectors are useless.

And complex inheritance trees are just bad. I should instead have been doing pure polymorphism, without inheritance. There's still a bit of overhead in writing a vtable, but there's no complexity. I spent so much time figuring out what methods go in which object, and shuffling them around from base classes to subclasses and back again... I should have learned to separate my algorithms from the objects they apply to.

I'm also finding that all the supposed advantages of C++ are useless or inferior to C. C++ encapsulation and header file management is even worse than C, because I have to expose my structure and private methods causing endless rebuild cycles. Creating callbacks is WAY more complicated than it should be (have to define an interface and then implement it instead of just handing over a function and struct pointer.) What is the point of all this?

The only feature I see myself using right now that isn't completely useless is overloading on types. Namespacing all my functions manually is a pain, and the argument that it helps readability doesn't really fly.

Lately I've been contemplating creating my own toy language, which is basically C except it doesn't use header files and allows overloading on types. There are tons of quicky compiler tutorials out there using tools like Flex+Bison; I am sure I could create a C99 parser which additionally allows overloading on argument types, and simply outputs C99 with header files and fully namespaced functions (or just calls LLVM directly).

Ironically this sounds a lot like Go, except I find Go has made a whole lot of bad decisions: garbage collection is a horrible idea for a systems/game language, enforcing encapsulation / exporting functions via naming conventions (uppercase wtf??), whitespace is starting to matter (notice what they're doing with removing semicolons, and all the bullshit wierdo special cases, like where you have to leave an extra trailing comma if you break up your argument list in different lines).

Would a language like this interest anyone? Should I just keep my project the way it is, half C++ and half C? What to do?

Name: Anonymous 2010-04-08 9:08

Name: Anonymous 2010-04-08 10:35

>>40-41
Thanks.

Name: Anonymous 2010-04-08 12:25

>>40
This is correct, but in addition, the compiler can also re-order computations performed on different types.

I don't actually like the strict aliasing rule though, mainly because it is obsoleted by restrict, and because there are lots of times I'd like to break it. For instance it fucks with most object systems (for this reason you can't compile Objective-C at all with strict aliasing). It also has lots of holes; for instance allowing anything to alias with char* just because it's common.

I'd much rather turn it off and use restrict explicitly in those few inner loops that have actually been shown to give a measurable performance boost. Explicit is better than implicit; it's always better to assume nothing and just execute the code as written.

Name: Anonymous 2010-04-08 12:27

>>43
*that should read 'to char*', not 'with char*', since you can't cast it back again.

Name: Anonymous 2010-04-08 19:31

>>43
C really likes to reserve the rights to call the shots for optimizations. You can make suggestions in the code about how to optimize, but you really only get to turn it down (with a few rare exceptions) on the command line. I agree that explicit optimization is really nice, but I see where they're coming from--it's a can of worms best left untasted.

Name: Anonymous 2010-04-08 19:33

>>45
CL has explicit optimizations/declarations to a certain extent, but not all assumptions you make are portable. Overall, it's a good thing.

Name: Anonymous 2010-04-08 19:55

>>45
Good job 45-san, searching for "a can of worms best left untasted" on Google will soon yield this page.

Name: Anonymous 2010-04-08 20:06

>>46
The can of worms I referred to is restricted to C. C rarely aims to compile to the sequence you wrote, but one that will have the same effect in the end. If you want to keep restrictions explicit, the same argument can apply to just about everything else, and suddenly you need to adorn every declaration with indications of how it should be optimized.

In each and every case I can see the benefits, but when you add them all up you have a problem. If that is what C was today, someone would invent a new language that took care of it implicitly. And it would be a hot topic. So I'm in favor of having a typical (default) optimization profile that can be tweaked from the command line, and fine-grained in the language. Really, C isn't far off from that (if you go off spec), but it doesn't seem to be complete in any implementation.

but not all assumptions you make are portable
Is that 'you' as in >>45? - or 'you' as in 'the programmer'? Which assumptions are these? The ones I make are that I will have to retain and review the assembly output if I want to know what is really going on. I'm paranoid enough in this that I maintain the assumption across compiler invocations of the same version, on the same system.

Name: Anonymous 2010-04-08 22:09

>>47
How soon? It's been like 2 hours already and still no result.

Name: Anonymous 2010-04-08 22:23

>>49
Blizzard's ``soon''.

Name: Anonymous 2010-04-08 23:22

>>50
wwwww

Name: Anonymous 2010-04-09 0:15

>>50
We used to call that "Real Soon Now" (RSN).

Name: Anonymous 2010-04-09 0:19

>>50-52
You children and your video games.

Name: Anonymous 2010-04-09 0:23

>>53
What! Listen here, jerkface!

Name: Anonymous 2010-04-09 0:29

>>53
``RSN'' predates Blizzard, dude. Hey, at least I don't play with dolls anymore.

Name: Anonymous 2010-04-09 6:11


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