开发者

What are the differences between using pure C with a C compiler and the "C part" of a C++ compiler?

I'm not sure what programming in C really means: - Programming in pure C with a C compiler or - programming in C with a C++ compiler.

Apart from the differences between the C's syntax of C and the C's syntax of C++, can I safely say there are absolutely (or in very few cases) no differences between two executables in terms of performance ?

I'm thinking about this issue, because in game programming, each one of the rendering part, the game object part and the game scripting part can be programmed completely different languages, to obtain the best compromise between execution speed and easy development, and this at each one of those part.

This separation between parts can be important for me, for example开发者_StackOverflow中文版, I want to make a versatile 3D adventure engine, where a community would make their own gameplay without having to mess with the engine. It would only be able to make games with a single character and several ennemies, so different game type would be covered: hack & slash, infiltration, RPG, platform, etc.

I should have put this 2 paragraphs in gamedev.stackexchange, but the first part is only about languages...


There are a lot of minor nitpicks. One that strikes me as being the most obvious is that in C++, you have to cast the return value of malloc. Also structs are automatically typedefed in C++.

Always use a C compiler for C code, not C++. C++ isn't perfectly compatible with C.

A few others:

  • In C, declaring void func(); declares a function that hasn't specifed what its arguments are, whereas in C++, void func(); is equivalent to the C void func(void)', taking no arguments;
  • Prototypes are required in C++, whereas it's generally just a warning in C;
  • The type of character constants (like 'a') is int in C and char in C++;
  • The type of string literals is char [] in C and const char [] in C++;
  • Some legitimate variable names in C, like class, are reserved keywords in C++.

For all those who don't believe me and are downvoting, check out this C code:

#include <stdlib.h>

int main(int argc, char **argv) {
    int *i = malloc(sizeof(int));

    return 0;
}

Compilation under gcc is fine, but compilation under g++ gives the following errors:

test.c: In function `int main(int, char**)':
test.c:4: error: invalid conversion from `void*' to `int*'


Note: The differences between C and C++ syntax are already described on other posts... Still, something bothered me enough to prompt the following answer:

If I understood correctly, you want to have two separate parts in a program, one in C and one in C++. One supposed should have to be really fast, and the other could be slower.

In the current case (comparing C and C++ performance), there will be no visible difference if the same C code is compiled in with a C and in C++ compiler...

Of course, never underestimate how the skills of the programmer are important for the performance of a program, no matter the language.

Choosing a C compiler

Pros

  • If you're lucky (using a recent gcc, or whatever), you'll be able to use C99 new features (note that C++ has most useful parts of C99 already available, either as native to the language, or in the standard library).
  • You won't use a C++ feature by mistake, and thus, can safely bet you won't have surprises outside the K&R

Cons

  • You won't be able to use C++ features
  • Not every C compiler supports C99 (for example, Visual C++ is working hard to implement the newly C++0x standard, but did little work to implement C99)... So you could be stuck with C89 code if you work with, or target the wrong compiler.

Choosing a C++ compiler

Pros

  • You'll have access to both C and C++ libraries
  • You'll be able to use the STL and Boost
  • You'll be able to write templated code (i.e. faster and safer than their void * counterparts).
  • You'll be able to write all your code in C, excluding some minor details (C++ disallow implicit casting from void *, etc.). Fact is, the "minor details" above are considered as dangerous, which is why they generate errors or warnings on a C++ compiler.

Cons

  • If you want to export functions with the C naming convention, you'll have to use the extern "c" specifier.
  • You won't be able to implicitly cast from void * (note that this is not supposed to happen often, or even at all, in C++, so this a negligible problem when compared with potential casting errors)
  • If you write C++ code, then you'll have to learn a lot more than simple C to get it right (RAII, constructors/destructors, exceptions, etc.)

Producing C/C++ code

By C/C++, I mean code that will be correctly understood by both C and C++ compilers. While your language of choice could vary, those compatible C/C++ header will be the same (even if you code in C++ and will provide additional C++ headers for C++ users of your code)

For your C code to be compatible with others' C++ code:

  • decorate your function declarations with an extern "C" specifier, wrapped with #ifdef __cpluplus. This will make sure a C++ compiler will know those functions are exported as C functions
  • If you use them, don't let C99 features ever be seen by the C++ compiler. Some of those features will never ever be supported by any C++ compiler. Fact is, some major compilers won't even support C99 for their C compilers (see http://en.wikipedia.org/wiki/C99#Implementations)
  • Avoid using C++ keywords, or at least, don't let the C++ compiler see them (i.e. exporting a function called namespace or class or template is a bad idea)

For your C++ code to be compatible with others' C code:

  • provide alternative headers and functions wrapping C++ classes and functions. Don't punish the C++ folks by removing classes, etc., just because you want to remain compatible with C, but in the other hand, make sure the C folks will have reasonable access to your library without moving to a C++ compiler.
  • In the headers writen for the C folks, decorate your function declarations with an extern "C" specifier, wrapped with #ifdef __cpluplus. This will make sure a C++ compiler will know those functions are to be exported as C functions

Additional info

I found the following page quite interesting, as it lists the differences between C (including C99) and C++:

http://david.tribble.com/text/cdiffs.htm

As for C99 features missing from C++, you can read my answer to the question What can be done in c but not c++?: I describe C++ features easily replacing those C99 features.

Afterwords

Anyway, if C++ is considered fast and robust enough for the F-35, then it should be enough for you.

Much of the F-35's software is written in C and C++ because of programmer availability; Ada83 code also is reused from the F-22.

Source: Wikipedia : https://en.wikipedia.org/wiki/Lockheed_Martin_F-35_Lightning_II

So, if you need to choose, then choose your language because you like it, or because one as something the other has not. But not because of supposed performance difference.


I think there may be a tiny difference but often in programs the algorithms is the place where you should put most effort so whether you go C or C++ doesn't matter from the performance point of view (IMHO). C++ allows you to easier(*) abstract your model creating a more user friendly framework so even if there was a difference I wouldn't worry too much about that.

(*) i.e. better supported by the language itself.


Never mix this two languages, they are very different. Even in programming in "C" style you can accidentally make big mistakes by using C++ constructs.

For C software use a good C compiler that supports C99 like gcc or intel. For C++ use a good C++ compiler that does the job. Mixing them would lead to bad and dangerous code.

Example?

You see the code is in file ".cpp" and start using simple thing like std::vector that throws exceptions (for example std::vector::at... Exceptions in C code would be a disaster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜