Why C++?

Started by
22 comments, last by lee101 10 years, 1 month ago

So I dabble with programming, and I know some C++, C# and a little about java/javascript and HTML, and while searching around I frequently found people that say that "real game programmers" use C++ for game programming. However from what I gather even if that seems to be true, there are a lot of other programming languages, perhaps even more suitable for game programming.

So then I happened upon somewhat of a dumb but meaningful question:

Why C++?

What makes C++ the favorite/mainstream language in game programming, when other languages such as C# or javascript are easier? Is it just because C++ has been around longer? Does it offer more control over some aspects of data-storage and such? Does it simply have more support?

Thank you in advance for answering.

Advertisement

Native programming languages are preferred for critical performance code since they have a better memory management and (sometimes) less overhead than managed programming languages.

C++ is often preferred than C (in the game industry) since it is a multi-paradigm programming language.

Finally C++ has a huge set of solid and tested tools.

Note also that AAA games are usually (always?) built using different programming languages (managed and scripting too) for different purposes.

"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/

I think there arent many choice there!

If we count there are only java,C++,C#,Python, Delphi... Java is only good for their embedded type, run well on many platform. But i think they are not fast as C++ and they are not very direct (in means of directX...). I worked alot with java, for server and j2me!

I'm not a master of C#, code couple of samples... They are very good for tool, windows app... that need good UI. The code is a lot easy and clear than C++ but they still work behind something (virtual machine???) like java. But I think it's very close compare to C++.

But even other lang is good for game, C++ is still superior because of the documents/samples/tutorials on the net, most of them are on C++ ! But using engines like Unity can save us a lot of time learning langs, they can export into many platforms (web,ios,pc...). Just learn the script is sometime enough (not all the time)!

I recall one of the issues with some of the languages listed (versus C++) is you dont have tight control of garbage cleanup. You leave it to the default and it kicks in whenever it feels like it. You control it and now you have to do alot of work because of the standard paradigm of programmer used to leaving it to automatic OR you program as if there is no automatic and you might as well be using C++.

Performancewise anything that uses a virtual machine will be slower. Can a JIT compiler be anywhere as good as a full blown dedicated native compiler ? and games do weird stuff the JIT might not be so great at optimizing (you can find the debates elsewhere).

I dont know if there is any need still to go to assembly code for critical chunks of code (that once was true).

Scripting languages ARE used for certain tasks and you would probably have to ask project manager what total coding line counts are used these days between the non-scripted (C++) versus scripting like LUA, etc...

Another factor is how old are the key architects of these games? Which languages were mature when they reached their highest coding efficiency (besides the other issues already mentioned)

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

If you were to try and re-implement something like Unreal Engine 4 in Java or C# it might become apparent why the industry uses C++ instead.

For simple indie games, I don't suppose it maters quite as much.

So I dabble with programming, and I know some C++, C# and a little about java/javascript and HTML, and while searching around I frequently found people that say that "real game programmers" use C++ for game programming. However from what I gather even if that seems to be true, there are a lot of other programming languages, perhaps even more suitable for game programming.

So then I happened upon somewhat of a dumb but meaningful question:

Why C++?

What makes C++ the favorite/mainstream language in game programming, when other languages such as C# or javascript are easier? Is it just because C++ has been around longer? Does it offer more control over some aspects of data-storage and such? Does it simply have more support?

Thank you in advance for answering.

Languages have "levels" which imply their "distance" from the hardware.

Programming in Assembly for example, you'd be really close to machine code, messing with how the hardware really works, so there's a lot more work to be done.

Higher level languages such as C# or Java have gone through tons of layers of code already, simplifying everything.

For example, creating a string in C# would be as simple as:


string myString = "someString";

In C, without the help of any libraries, you'd have to deal with an array of single characters:


char myString[20];
myString[0] = 's';
myString[1] = 'o';
myString[2] = 'm';
myString[3] = 'e';
//etc

(There's easier ways to initialize the variable, but the work is basically the above code)

So, the higher the language level, simpler the code management. But exactly because of this, you lose some "control" of what really happens.

For example videogames. Unlike desktop PCs, they have specific hardware set: Memory X, CPU Y.

When programming a game for a videogame you have to be aware of how much memory and CPU you have available and work with it.

In Javascript, you create variables, use them and that's it.

In C# and Java too, because they have something called "garbage collectors".

So what would happen if you keep constantly creating more and more variables and you reach the limit memory available? Crash. No more space for variables means no more variables, so expect something to crash.

In C++ and below, you can control when to delete a variable from memory, so you can make sure you'll have enough memory to continue your program.

"Garbage Collectors" in Java/C# free the memory when they detect that a variable isn't being used anymore. How do they detect it? This means there's another program running besides it checking for all variables you're creating and constantly checking them to see if they're okay to delete. This program is obviously consuming resources (CPU and Memory) as well, so that's why these languages are considered "heavier" on performance.

So, in general, the higher the language level, the heavier it is on performance because of all the things it is already doing to simplify the programmer's work.

When you want games to be as fast as possible, pushing the hardware to the limits, you don't want anything else doing (unnecessary) work for you.

Another thing about languages is that, the closer it is to the hardware (so the lower it's level), the easier it is to port to different machines.

All machines work in the same way in the lowest level possible: machine code.

The higher the level of the language, more layers it has been gone through, so more limitations it has.

For example Javascript, you must have a browser to run a javascript code (windows can work around it as well).

Or C#/Java, you must have their frameworks installed to run them (.NET and JVM), otherwise they just won't run.

So they're limited by whatever layers precede them. In comparison, an Assembly code could be run nearly anywhere - it's just a matter of how.

Taking all of these in consideration, you can figure why C/C++ is the language of choice for game programming.

It's not hard (harder than languages higher than it, obviously, but much much much easier than Assembly and lower languages), provides much control, and works nearly everywhere (where it doesn't is because the manufacturer is lazy), making your code much more portable.

But don't forget the famous "use the right tool for the job" quote. It'd be a waste of time to program in Assembly for a Windows Game just as it would be unproductive to program a game for PS3 using Javascript (not to mention the work it'd be just to have Javascript useable in the machine).

So I dabble with programming, and I know some C++, C# and a little about java/javascript and HTML, and while searching around I frequently found people that say that "real game programmers" use C++ for game programming. However from what I gather even if that seems to be true, there are a lot of other programming languages, perhaps even more suitable for game programming.

So then I happened upon somewhat of a dumb but meaningful question:

Why C++?

What makes C++ the favorite/mainstream language in game programming, when other languages such as C# or javascript are easier? Is it just because C++ has been around longer? Does it offer more control over some aspects of data-storage and such? Does it simply have more support?

Thank you in advance for answering.

The reasons we use C++ are mostly because it allows access to program nearly to the metal, and take advantage of all hardware acceleration or talk directly to the hardware.

The other reasons are legacy as we moved away from assembler towards C so in the past we made another switch from C to C++ so that we could take advantage of Object Oriented programming features of the language. All these steps were made to make programmers more efficient but not lose the access to hardware and memory management and tight control over what the code is doing as Danicco points out.

Most tripple A companies still dont use modern C++ features, generally C++ is used as C with Classes. Templates, STL containers and such are preferred to not be used. Neatly all C++ exceptions are turned off in favour of error codes in most game engines.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

The simple answer is C++ saves me time. Being close to the metal is nice and all, as is control of freeing memory, but I'd drop those features in a heartbeat if there was a language with better development tools and libraries available for game programming than what exists for C++. The success of XNA+C#, until Microsoft abandoned it, demonstrates I am not alone in that attitude. C# was taking significant market share away from C++ for games development due to the strength of tools available.

Well, theres a reason why OpenGL and DirectX are implemented in C and not C#. If you want maximal efficiency, you have to be close to the hardware. C is pretty much the only popular systems language left. By systems language, I mean something that has a fairly predictable translation to machine instructions, basically something you can use to write an operating system or hardware interface. The only thing lower are assembly (which is non-portable and very difficult to write) VHDL and Verilog, which describe hardware but not abstract it.

C++ is basically C with OO attached. You are free to use any C function, and many do.

Which is party of the problem and why C++ is unfortunately, quite defective. Here is a summary of the arguments http://yosefk.com/c++fqa/defective.html

To get around the deficiencies of C++, many completely omit the OO features of C++ (such as not using virtual functions, using trivial constructors/destructors and calling manual initialization and shutdown routines) At which point, you lose all the benefits of OO anyway, pn top of which you have a bunch of useless, extra code. I like to call this "C++--" In many cases, I also encounter what I consider to be lazy design patterns such as the singleton and factory which breaks OO methodologies (inheritance and the idea of reusable code), which again, leads one to question why we are using OO in the first place.

About the only good thing with C++ are the typesafe containers implemented in the STL. Still, I fiind them overally verbose when using the iterators and the code just looks ugly. Here's a gem from Ogre code I wrote a while back:


 
std::vector<MovableObject*>::const_iterator it = movingObjs.begin();
std::vector<MovableObject*>::const_iterator end = movingObjs.end();
while(it != end) {
      MovableObject* movableObject = (*it);
// do somethign with the movable object
      ++it;
}
Tell me how that is better than something like

 
for(int n=0; n<movingObjects.size; n++) {
      MovableObject* movableObject = movingObjects.objs[n];
// do something with the movable object
}
Assuming I have a proper movingObjects struct (not class) hich has the appropriate fields.

This topic is closed to new replies.

Advertisement