C vs C++

From WikiVS, the open comparison website
Jump to: navigation, search


Contents

[edit] Clearing misconceptions

  • C++ is not just C with object orientation. C++ brings huge libraries aimed at easing development. Despite its academic roots, C++ has been qualified as bloated by prominent experts:

"C++ is an insult to the human brain" (Niklaus Wirth)

"I invented the term Object-Oriented, and I can tell you I did not have C++ in mind" (Alan Kay)

"There are only two things wrong with C++: The initial concept and the implementation" (Bertrand Meyer)

"Whenever the C++ language designers had two competing ideas as to how they should solve some problem, they said, 'OK, we'll do them both'. So the language is too baroque for my taste" (Donald E Knuth)

[edit] Performance

Most C and C++ implementations compile code to the native CPU code. Utilizing C++ features such as virtual methods are thought to lead to more bloated applications than a more static, C style implementation. However, this is not actually the case in modern computers and compilers. C++ compilers and hardware architectures have been optimized for pointer dereferencing, which is the operation that virtual methods employ. Compilers may also optimize out virtual method calls entirely. One feature that does have performance implications is exceptions. Misuse of the exception mechanism can have a performance impact.

C++ was designed with a "no overhead rule", meaning that code will not suffer from overhead of features not used. Therefore it is theorically possible to write performance critical code in a procedural style that will run as fast as the comparable C program. But in practice, this may not always be the case simply because both C and C++ runtime libraries will be used by a single program.

[edit] Available Libraries

Many libraries have APIs for both C and C++. Usually, one of them is a wrapper for the other, most often the C++ API being a wrapper around the C API.

Libraries Available in C and C++
Library C C++
Gtk+ Yes Wrapper as GTKmm
Qt Bindings as Qt-C Yes

[edit] Software Implementations

Some projects use both C and C++ in their code. Because C is older and more polished, it is implemented more in critical applications and system components. Although C was the first choice in game programming some time ago, C++, in large part, has now taken over the commercial game industry.

Software in C/C++
Software C or C++
Linux Kernel C
Xorg C
Compiz C++
Firefox C++
MySQL both
PostgreSQL C
GNU Compiler Collection C, being migrated to C++
LLVM + Clang C++

Programming language interpreters and compilers on von Neumann architecture systems are typically implemented using C, including C++ compilers.

[edit] Server-Side Scripting

Just like Java or PHP, ANSI C scripts have been used to implement Web Application Servers with scripts executed on-the-fly Template:Citation needed.

[edit] Features

Because C++ is a language newer than C, and designed to be an improvement on C, it contains language constructs not present in C, while maintaining all of the original C functionality. Some features from C++ were put into the C99 standard. Features in C++03 that are not in C99 include:

  • Templates
  • Streams
  • Inheritance
  • Class encapsulation
  • Virtual methods
  • Overloaded operators
  • Reference passing
  • Exceptions
  • Namespaces

Naive use of some of these features, such as exception handling, can slow down programs or significantly bloat them, even making them unusable.

Usage of some of these features is far from obvious. There are many "novice errors" due to misunderstanding of exact meaning of some C++ operators.

There is a popular misunderstanding that the sole fact that those features must be loaded in memory (as part of the runtime) is handicapping C++ (as compared to C) when bare-performances are the target. This is not always true and depends on the developer's experience, since those features are usually processed at compile-time, not at runtime. In many cases usage of C++ instead of C results in a smaller and more efficient code with smaller memory footprint due to more advanced use of compile-time features. However not every developer is able to write code of such quality, so most of the existing C++ code in the world probably performs worse than C code in general.

[edit] Learning curves and gotchas

The most spectacular difference between C and C++ is the length of the specifications. C++ required the font size to be heavily reduced to permit that the book had a reasonable number of pages.

In C++, the challenge is to learn how to use the language features in the most useful and safe way. In C, you have to have a bit more knowledge on how things work at the lower level. As an example, in C, you have to know how pointers work to do advanced operations. In C++, clearer but more abstract ideas have been introduced including references. C-style pointers are also available. Pointers are among the most difficult concepts for a beginner to grasp.

Use of raw pointers in C tends to be much more straightforward than in C++, however. C++ is typically used when a developer wishes to have access to standardized object oriented programming abstractions, but incautiously mixing such abstractions with use of pointers to allocated memory can more easily result in memory leaks and segmentation faults. A program that is developed with C++ features in mind can make use of shared pointers (for object members and in function bodies) which provide a method of automatically releasing memory when an object goes out of scope. This relieves the programmer of the need to manually call delete or free() in destructors or after a variable is no longer referred to. Other useful bookkeeping mechanisms like auto_ptr and smart_ptr have also been created.

As in many object-oriented languages, the possibility of circular references (where two objects maintain a reference to each other) can cause the objects to never be released from memory. If shared pointers are used, one must explicitly delete one of the objects in a circular dependency. Weak shared pointers also may be used to break this cycle. One has to mention that "native" C++ references have nothing to do with memory allocation and thus can be misused: there IS a way to create a reference to non-existing object.

The "straightforward" C programming may be also considered "gotcha" by some developers. That is, since C forces to write "straightforward" code, there is no simple way to use "generic" code. Usually that leads to massive use of copy and paste. Sometimes non-obvious exception handling methods are also used, like longjmp and goto. A developer's mistake in such a code always leads to critical errors like memory leaks, crashes or, even worse, security holes.

Both languages have similar problems of object creation and destruction performance. C has malloc and free, while C++ has new and delete. Both can be misused, both can lead to memory leaks or significant slow-down. But C++ is much easier to misuse due to semantics of its operators. These operators are written similarly to the ones of Java or Delphi but differ dramatically in their semantics. This is a very common trap for the person who shifts to C++ from Java or Delphi. It is even more dangerous since it is not always identified by the debugger.

[edit] Links

[edit] Pro C

[edit] Pro C++

See Also the Following Articles

Personal tools
Namespaces
Variants
Actions
Navigation
Ads
Toolbox