Sunday, November 22, 2009

Advantages of C over C++

It has been claimed that C++ is a better C them C. this is being taken to mean that when switching to C++ you can continue to code more or less as she did in C and use a little extra C++ functionality for convenience. The problem with that is that a lot of things which are perfectly safe to do and see are not safe to do while using C++. So here is my list of issues not found in C. You can avoid many of these issues in C++ by limiting what features you use. But you never have any guarantees. You can't pick up random C++ code, look at it and be certain whether it is doing something safe or not when e.g. statically initializing a variable.
  • Static initialize is safe in C but not in C++, because in C++ static initialization can cause code to run, which depends on other variables having been statically initialized. It can also cause cleanup code to run at shutdown which you can't control sequence of (destructors).
  • C gives you better control over what happens when your code is executed. When reading seek out it is fairly straightforward to decipher one code is getting executed and when memory is just restart or primitive operations are performed. In C++ on the other hand your have to deal with several potential problems:
    • A simple variable definition can cause code to run (constructors and instructors)
    • Implicitly generated and called functions. If you didn't define constructors, destructors and operator= you will get them generated for you.
    See hidden cost of C++ or Defective C++
  • C supports variable sized arrays on the stack. Which is much faster to allocate than on the heap. (C99 feature)
  • No name mangling. If you intend to read generated assembly code, this makes that much easier. It can be useful when trying to optimize code.
  • De facto standard application binary interface (ABI). Code produced by different compilers can easily be combined.
  • Much easier to interface with other languages. A lot of languages will let you call C functions directly. Binding to a C++ library is usually a much more elaborate job.
  • Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++.
  • Varargs cannot safely be used in C++. They're not entirely safe in in C either. However they're much more so in the C++, to the point that they are prohibited in the C++ coding standards (Sutter, Alexandrescu).
  • C requires less runtime support. Makes it more suitable for low-level environments such as embedded systems or OS components.
  • Standard way in C to do encapsulation is to forward declare a struct and only allow access to its data through functions. This method also creates compile time encapsulation. Compile time encapsulation allows us to change the data structures members without recompilation of client code (other code using our interface). The standard way of doing encapsulation C++ on the other hand (using classes) requires recompilation of client code when adding or removing private member variables.
Disliking C++ is not a fringe thing. It does not mean that one is not capable of understanding complex languages. Quite a lot of respect computer science people and language designers aren't fond of C++. See C++ coders

4 comments:

Issam said...

Both languages are great!
Once you got used to C++, it does not only help you abstract things in a strait forward fashion, but even apply old devil C practices: Follow on with Assembly output and figure out what the compiler is doing!

You can always use a lightweight C++ and avoid all the greedy features of its standard.

BTW, I have never considered C++ as a different language than C!

Unknown said...

When starting a new project, consider using the D language instead of either C or C++. D is a C-like, compiled language that is suitable for system level programming. It also integrates nicely with C because it can link with C binaries. For more info check out these websites:
* http://www.linux.com/archive/feature/124320
* http://www.digitalmars.com/d/2.0/overview.html
* http://en.wikipedia.org/wiki/D_(programming_language)

antred said...

Disadvantages of C as compared to C++:

1) no RAII (ifyou've been coding for more than 2 years but don't know what that is, do the world a favor and find a new hobby / profession) ... this is one of THE most essential tools a language can offer

2) horrible type safety

3) next to no support for generic programming ... if you want genericity in C you'll be casting to void* and back until the cows come home ... see point 2

4) having 'total control' is simply just a euphemism for having to reinvent the wheel from scratch over and over again because C offers you fuck all for tools, whereas C++ allows you to get stuff done with a fraction of the code

Calmarius said...

I jump back and forth between C++ and C projects. Here is my thoughts:

C++ is easy to write, because the RAII and the lot of implicit stuff and its standard library save a lot of work when writing code.

But this is its disadvantage too: implicit code execution caused by overloaded operators, temporary objects, exceptions may make difficult understand what happens in your code when someone just debugging into your code without knowing your code and your types.

C is tedious to write, because you must do error checking, resource allocation and release by hand everywhere. But at least it makes debugging easier, because the entire logic unfolds before your eyes, you can see what happens, because there is no implicit code execution anywhere.

In the industry you write the code once but you need to maintain it forever, C is easier to maintain and debug than C++.

The brevity of C++ may make you more productive, but definitely a disadvantage when someone needs to maintain your code in the next years.