The Problem With C++

Eli – Delphi does have a ‘form’ of garbage collection: when you destroy an object, all objects that have this as their parent are destroyed first. E.g., when you dispose of a TForm object, all buttons, listboxes, etc. on the form are disposed of first.

Manually instantiated objects must be destroyed.

Certain languages/environments are more dangerous than others. Look at all the buffer exploits for C code, or all of the SQL Injection problems with ASP (classic) or PHP. (Or the performance problems with Java strings in early versions of Java.) While C++ is definately safer than C, it’s still more dangerous than C#, Java, or Delphi.

C++ certainly has its faults, but if you can stay out of the worse parts of the neighborhood and use decent quality practices, you can write some very good software in it. I’ve never bought into the “era of abundance” paradigm, since it encourages sloppy practice and general cycle-wasting.

Wasted cycles cost money, in case you’d forgotten that. It’s the “I’m going to drive an SUV because it’s ‘safer’” argument, while blithely ignoring that the ‘safety’ costs minimally twice as much to maintain as with any other vehicle; a well-built, safety-conscious smaller vehicle would be perfectly viable–just so long as you don’t have the larger gas-guzzlers careering all over the interstate.

Languages that get more done in fewer cycles cost less to run at the plug. Arguments over maintainability will seesaw back and forth over the decades, and that’s a separate set of costs, anyway. (SUVs cost more to repair, too, if you want the analogy.)

This is not to say that there isn’t some other middle ground. C# is very well thought-out, and very easy to learn for a C++ programmer. (Maybe even for Java programmers, but I don’t see a lot of them volunteering.) Not sure about the economics of C# yet, but I’m thinking about it.

"But we’ve long since left that behind; we live in an era of abundance. We have more computer power than we possibly know what to do with on the desktop. "

Ok - that bit really worries me! Why not just advocate badly written code, hell the machine can cope!

But this point:
"I think [making computer languages easier for average people] would be misguided. The idea of programming as a semiskilled task, practiced by people with a few months’ training, is dangerous."
is one very well made! I am fed up of dealing with “developers” who have learnt a smattering of php/python or heaven forbid Ruby on Rails it is a real big problem these days - too many hobbyists!

I’ve not used C# so I really cant comment on it. I do use Java quite a lot and find it does the job well enough…but C++ is my langauge of choice. A well-educated C++ programmer has nothing to fear!

I’ve always sort of considered C++ the library language. The only way to really glue a language to an OS is to use C++ to create the libraries and such. I mean, look at anything ported from Linux to Windows: the only way they work is through DLLs (or using CygWin or a few other ideas that are less easy for the novice), which are in turn coded through C++. The majority of the DLLs are careful ports of C++ files. C++ is such an integral part of the low-language structure of most OSes that you really can’t side-step its use. I mean, can you imagine trying to do anything serious on Linux without gcc?

My frustration is that there are still a number of game programming libraries that require C++, because that’s one of the places where speed is still VERY much a factor. You just don’t have the same superior output from most other languages as you get with C++… and that’s just kinda sad.

There will always be projects where you need as much speed as possible, partly because what you undertake gets more ambitious as machines get faster.

Also, you’ll often see parallelization cited as the cure for Moore’s Law coming to an end, or at least having reached a flat spot. Well, not all algorithms are parallelizable, and when they are, it’s frequently hard to do so, and you lose total CPU efficiency, putting pressure back onto optimizing the heck out of everything. So, the need for speed will never go away, and one might even project from this that the use of C++ will grow!

As for the idea that C/C++ are no longer relevant, what do you think the application you are reading this with is written in? You may have some new-fangled child-safety language working on the server side, and JavaScript on the client end, but C/C++ is there everywhere. Your browser is written in C++, running on an operating system with C at the core, and a good chunk of websites are served with Apache running on Linux.

Take the cement/concrete analogy and run with it a bit. As a new building using ready made carbon fiber panels (mentioned above) rests on a foundation of concrete, so too does your Web application written in [insert fashionable language] rest on a foundation of C.

Maybe we don’t need more experts to learn to program close to the hardware. We need just enough to get the hard ‘dirty’ work done so the rest can do their work. There will always be a need for systems-level programming, the question is how much.

I have never had a problem with C++ and found it more elegant compared to C, but I think that once I discovered Ada (and I am talking the first iteration of Ada, not Ada 95) I picked that up and never looked back.

There is a WHOLE LOT of truth in the fact that if you are going to develop with a language as powerful as C, you need to know what you are doing… design design design. Same goes for a language that is C but now with more complicated interfaces that wrap underlying code… C++ becomes more dangerous in that respect.

It’s stupid to make pronouncements as if there is one true language for everything. I would never build a web site in C++. I would never build a GUI in PHP. This is a topic as ridiculous as Mac vs. Windows. Use the right tool for the job or go home.

I just want to correct a couple things:

  1. Delphi/Object Pascal doesn’t have garbage collection, except possibly in the .NET version. When the language itself is compiled to native code, you do manual memory management. It’s still far easier than C++, though I sometimes wish for stack-allocated class instances.
  2. C++ blows for kernel programming. See http://www.osdever.net/tutorials/pdf/cpp_kernel.pdf and http://www.osdev.org/wiki/C_PlusPlus. Notice the sheer quantity of runtime work you have to do before actually compiling a kernel with full C++ features. I write a kernel in Object Pascal, and I had to do only a small fraction of that to make a working RTL under the Free Pascal Compiler that would run on bare metal.

C++ is really a language for driver programming or low-level applications programming. Don’t try to take it lower or higher than those.

Many people seem to think C++'s niche is ‘systems programming’ and ‘Devices drivers’. That may be true for user-mode level programming. However, C++ has some issues with programing anything in kernel mode. Check out this blog by Adi Oltean, and the white paper it references:

http://blogs.msdn.com/adioltean/archive/2005/04/24/411356.aspx

whitepaper:
http://www.microsoft.com/whdc/driver/kernel/KMcode.mspx

• The use of the C++ compiler as a “super-C” is typically expected to work, but such use of the compiler is at the developer’s own risk.

• It is currently impractical to identify problematic C++ constructs mechanically, so developers must carefully analyze the compiler output to ensure that the generated code is suitable for kernel mode.

• Before committing to the use of C++, you should carefully assess whether it will work for you. In particular, you should test C++ constructs early in the development process, to ensure that the constructs do not cause the problems described in this paper or otherwise violate the principles of kernel-mode driver writing.

• Some of the problems discussed in this paper might not become apparent until near the end of development, and that solving them might require code to be completely rewritten.

• Several of the most insidious problems are extremely difficult to reproduce on demand while testing the driver, so a driver with an inherent unreliability might appear to run for extended periods with no problems and fail at random times. This reinforces the need for careful analysis.

The bullet points in my previous post are quoted from the whitepaper. I should have been more careful to make that apparent.

Funny to see people still using VB as a whipping boy when there hasn’t been a new release in over 8 years!

C++ is and will continue to be relevant because it has no real competition in its problem space today. That doesn’t say anything about its quality one way or another.

Other tools encroach on the fringes a bit, and every day we seem to see new introductions with potential. They just aren’t there yet and sometimes that’s simply a question of momentum.

I see C++ as an incredibly unproductive language choice, but when the alternative is simple C or even assembly level coding it is just too hard to argue with. For low-level work it continues to hold a significant advantage.

That doesn’t mean a contender based on Algol couldn’t invade and compete in this space. Delphi has done well in making inroads partly into C++'s domain and FreePascal has had some benefit in tearing down the wall of propriety around Delphi. Other languages descended through Pascal haven’t achieved so much, mostly because of excess academic baggage along with relative obscurity and rapid churn and divergence.

Stability is one of the best things C++ has going for it. Change has been evolutionary at a pace the industry has been able to absorb and implementations aren’t so divergent as to cause unnecessary headaches.

“That’s exactly why I like C# so much. It’s just too bad that we are restricted to one single platform by using it :(”

My friend, you CAN run C#.NET programs on Linux/Mac/Solaris machines.

http://www.mono-project.com/Main_Page for more information.

Article made the front page on Digg.

“too many software developers are undereducated and undertrained.”

What a lame statement. Same could be said for people that like to code in C/C++ instead of ASM. Languages do not evolve to meet the needs of the lesser educated, how can such a smart guy make such dumb statement.

Portuguese translation of this article - summary.

C++ is OO assembly language. 5 years ago for the majority of apps there was no call for an __asm, now the same might be said for __c++ (sic).

Jeff Atwood wrote “… we live in an era of abundance. We have more computer power than we possibly know what to do with on the desktop.”

Even if it was true that “We have more computer power … on the desktop” then we might still have a scarcity of computer power on servers, on mobile phones, on sensors, on autos …

My company codes in c++, c, php and Eiffel to name a few. I have to say, Eiffel is seriously amazing. Check out the wikipedia link. Eiffel has Garbage collection, pure object orientation, it’s elegant and NOT based on C. Oh, and it’s open source. Check it out here actually rates just under C++ in speed, http://shootout.alioth.debian.org/gp4/benchmark.php?test=alllang=selang2=gpp