When should we use C over C++ ?

Pages: 12
rapidcoder wrote:
You have a C project - just hire C programmers and you are done - they can immediately start hacking.

Great quote. The "easy to find" programmers are indeed hack jobs. It's just as hard to find a competent C programmer as a competent C++ programmer.

To answer the OP
crazy frag wrote:
What are the cases when we should use C or Assembly over C++ ?

Normally, you would use C and not C++ when:

1. the project already has or expects a C language code base: POSIX-compatible OS kernel, typical (especially Linux-related) open-source software, general-purpose software that existed for the last 20 years, etc.

2. the project's target platform only has C compilers available. Even though C++ is more portable that C in theory (I'm serious), writing a C++ compiler is a high-cost job, so in practice C++ compilers are not available for many non-mainstream platforms.

3. the project's requirements need little besides interfacing the hardware (the "portable assembler" case) - some device drivers and firmware projects fall in this category. That's the kind of jobs C was meant for.

You use assembly languages instead of C or C++ when

1. The target platform has no C compiler or the overhead of C is unacceptable (e.g. you're writing a 256-byte demo)

2. The existing compilers do not meet your requirements (e.g. fail to utilize the available instruction set or fail to optimize efficiently)
C++ contains features for memory management and handling character strings that C does not have. I agree with the portability comments, however.
Although C and C++ are almost equally fast (C is slightly faster due to less fat), certain OO techniques employed in C++ actually increases speed substantially more.

If memory serves me right then I think if you measure the performance between most of the old c-string functions against the equivalent stl strings methods & operators, one would find the stl string faster. I believe this is due to it storing the length of the string as opposed to the c-string which purely uses a null terminator. The string object can therefor use this length to optimise on certain functions (like applying flat memcpy for a strcpy ...)

Such OO techniques will provide greater performance, but at the cost of more resources (like using an int for storing the length of the string). Thus if you are targetting an environment where resources isn't an issue, then I would actually suggest using C++ for performance and better coding constructs.

Even if one were to create some type of struct to emulate a C++ string object, it would still fall short of having the true potential of a real string object and would be very clumbsy to work with. Even if such a struct exists, I still recall myself using the plain old c-string when working in C way back and would asume that most (if not all) C developers today would still use the same.

I also personally think C++ really rocks for very scalable high performance multi-threaded systems (using threadpools, automatic lock releasers, thread abstractions, ...).
Topic archived. No new replies allowed.
Pages: 12