What is the difference between using pure C with the C compiler and the "C part" of the C ++ compiler?

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

Besides the differences between the C syntax of the C syntax and the C C ++ syntax, can I confidently say that there are absolutely (or in very few cases) differences between the two executable files in terms of performance?

I think about this problem, because in game programming, every part of the rendering, part of the game object and part of the game script can be programmed in completely different languages ​​to get the best compromise between execution speed and easy development, and this is in each of these parts.

This separation between the parts can be important for me, for example, I want to create a universal 3D adventure engine, where the community will create its own gameplay without having to mess with the engine. He could create games with only one character and several enthusiasts, so he would cover a different type of game: hacking and slash, infiltration, RPG, platform, etc.

I had to put these 2 paragraphs in gamedev.stackexchange, but the first part only concerns languages ​​...

+7
c ++ c
source share
4 answers

There are many small nitpiks. The one that seems most obvious to me is that in C ++ you must specify the return value of malloc . Also, structures are automatically typed in C ++.

Always use the C compiler for C code, not C ++. C ++ is not entirely compatible with C.

A few others:

  • In C, the declaration is void func(); declares a function that did not indicate what its arguments are, whereas in C ++ void func(); equivalent to C void func(void)' , without accepting arguments;
  • Prototypes are required in C ++, whereas this is usually a warning in C;
  • The type of character constants (for example, 'a' ) is int in C and char in C ++;
  • Type of string literals char [] in C and const char [] in C ++;
  • Some legitimate variable names in C, such as class , are reserved keywords in C ++.

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

 #include <stdlib.h> int main(int argc, char **argv) { int *i = malloc(sizeof(int)); return 0; } 

Compiling in gcc is good, but compiling under g++ gives the following errors:

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

Note. The differences between the syntax of C and C ++ are already described in other posts ... However, something bothered me enough to trigger the following answer:

If I understand correctly, you want to have two separate parts in the program: one in C and one in C ++. Each was supposed to be very fast and the other to be slower.

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

Of course, never underestimate how important programmer skills are for running a program, regardless of language.

Choosing a C Compiler

Pros

  • If you're lucky (using recent gcc or something else), you can use the new C99 features (note that C ++ has most of the useful parts of C99 that are already available, either as native language or standard library).
  • By mistake, you will not use the C ++ function, and thus, you can safely say that you will not have surprises outside of K & R

Against

  • You cannot use C ++ functions.
  • Not every C compiler supports C99 (for example, Visual C ++ is working to implement the new C ++ 0x standard, but has done a bit of work to implement C99) ... So you can get stuck with C89 if you work or aim for the wrong compiler.

Choosing a C ++ Compiler

Pros

  • You will have access to the C and C ++ libraries.
  • You can use STL and Boost
  • You can write boilerplate code (i.e. faster and safer than void * copies of it).
  • You can write all your code in C, with the exception of some minor details (C ++ prohibits implicit casting from void * , etc.). The fact is that the β€œminor details" above are considered dangerous, so they generate errors or warnings in the C ++ compiler.

Against

  • If you want to export functions using the C naming convention, you have to use the extern "c" qualifier.
  • You cannot indirectly drop from void * (note that this should not happen often or even in general in C ++, so this is a minor problem compared to potential casting errors)
  • If you write code in C ++, you will need to learn much more than just C in order to get it correctly ( RAII , constructors / destructors, exceptions, etc.)

C / C ++ Code Generation

In C / C ++, I mean code that will be correctly understood by both C compilers and C ++. Although your choice language may differ, these compatible C / C ++ headers will be the same (even if you are C ++ code and provide additional C ++ headers for C ++ users of your code)

If your C code is compatible with other users' C ++ code:

  • decorate function declarations with the extern "c" specifier wrapped in #ifdef __cpluplus . This will ensure that the C ++ compiler knows that these functions are exported as C. functions.
  • If you use them, do not let C99 functions ever see the C ++ compiler. Some of these functions will never be supported by any C ++ compiler. The fact is that some large compilers do not 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 (e.g. exporting a function named namespace or class or template is a bad idea)

In order for your C ++ code to be compatible with the C code of other users:

  • provide alternative headers and functions that wrap C ++ classes and functions. Do not punish C ++ people by deleting classes, etc., Just because you want to remain compatible with C, but, on the other hand, make sure that C people will have reasonable access to your library without going to the C compiler ++.
  • In the headings written for C people, decorate function declarations with the extern "c" specifier wrapped in #ifdef __cpluplus . This will make sure that the C ++ compiler knows that these functions should be exported as C. functions.

Additional Information

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 the C99 functions that are missing in C ++, you can read my answer to the question What can be done in c, but not C ++? : I describe the features of C ++ that easily replace those C99 functions.

Afterword

In any case, if C ++ is considered fast and reliable for the F-35 , that should be enough for you.

Most F-35 software is written in C and C ++ because of the availability of the program; Ada83 code is also reused with 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 it somehow has no other. But not because of the supposed difference in performance.

+7
source share

I think there may be a tiny difference, but often in programs, algorithms are the place where you should put maximum effort into it, so if you go to C or C ++ it doesn't matter in terms of performance (IMHO). C ++ allows you to simplify (*) the abstract model by creating a more user-friendly structure, so even if there was a difference, I would not worry too much about it.

(*), that is, is better supported by the language itself.

0
source share

Never mix these two languages, they are very different. Even when programming in the "C" style, you can erroneously make big mistakes using C ++ constructs.

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

Example

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

0
source share

All Articles