Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There's a quotation by Bjarne Stroustrup that says:

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

I've often come across this quotation when reading library documentation and tutorials; particularly ones that wrap C functions in C++ classes, or are wrappers to C libraries.

But what did he mean by this?


migration rejected from Dec 8 '15 at 17:17

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as primarily opinion-based by amon, Scant Roger, GlenH7 Dec 8 '15 at 17:17

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

This type of question is now being discussed on our meta-discussion site. – user8 Dec 5 '11 at 20:19
up vote 85 down vote accepted

The real answer is that, compared to C, C++ builds in safe-guards against common errors in C, for instance off-by-one errors or dereferencing invalid pointers, by removing the need for indices and pointers in most situations, and by improving type safety.

In general, you need to be quite explicit in C++ to circumvent security (e.g. by explicitly acquiring a pointer or casting away const-ness). But when you do that, the language lets you do it, and offers no diagnostics whatsoever: you venture into undefined behaviour land, where anything goes and everything will go wrong.

Due to the added complexity in C++, when things go wrong they often go wrong in a spectacular manner and with an error that is very hard to find because the cause is often very remote from the place where it fails.

Finally someone posted a serious answer. Thanks (+1). And I know what u mean about untraceable errors and undefined behaviour. I get hundreds of them daily. :D – ApprenticeHacker Jul 12 '11 at 7:27
@Konrad: if off by one error include dereferencing "end" iterators, I don't see much improvement wrt pointers :/ – Matthieu M. Jul 12 '11 at 7:54
@MatthieuM. Iterators denoting a range are an improvement over indices, not pointers. Containers and smart pointers are an improvement over (raw) pointers. – Konrad Rudolph Jul 12 '11 at 8:24
@Shadur: Was he unaware of the existence of for loops? – JAB Jul 12 '11 at 20:40
@Konrad By smart pointers do you mean std::auto_ptr and std::shared_ptr – ApprenticeHacker Jul 31 '11 at 2:52

Well... let's let Stroustrup himself answer you:

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off". Yes, I said something like that (in 1986 or so). What people tend to miss, is that what I said there about C++ is to a varying extent true for all powerful languages. As you protect people from simple dangers, they get themselves into new and less obvious problems. Someone who avoids the simple problems may simply be heading for a not-so-simple one. One problem with very supporting and protective environments is that the hard problems may be discovered too late or be too hard to remedy once discovered. Also, a rare problem is harder to find than a frequent one because you don't suspect it.

Quoted from:

The link does not see to work anymore. Looks like this one does though – Shafik Yaghmour Mar 11 at 16:29

To be honest, I think it's something he said because it sounded good, and, in a way, as good marketing for the language.

C++ makes it harder to shoot yourself in the foot, that part is true. C++ has stronger type safety, and abstractions and idioms such as RAII are there to eliminate many common error sources (such as memory leaks or double-frees) that, in C, would have led to you "shooting yourself in the foot".

But I really really can't see any way in which C++ allows you to do more damage when you do shoot yourself in the foot. I think that part was added because "it sounded catchy", and in a way, it makes C++ sound even more impressive and fascinating and powerful.

Yeah. Maybe it was said to make c++ more popular. (+1) – ApprenticeHacker Jul 12 '11 at 7:43
+1 good answer. I also can't see any way in which C++ allows you to do more damage when you do shoot yourself in the foot (with C). – Nawaz Jul 12 '11 at 7:45
What is meant here, I think, is the fact that UB triggers a cascade of bullshit. Destructors not called, virtual tables pointing to Nirvana, and nobody to warn you. For instance, in C you’d need to explicitly free any object that you no longer need. In C++, you might rely on a destructor … but oops, it was nonvirtual and now it only freed half your memory and left the parent in an unreachable state. – Konrad Rudolph Jul 12 '11 at 8:25
@Konrad: perhaps, but again, I don't really see this as causing more damage than the equivalent problems in C, as the bazooka analogy would seem to imply. – jalf Jul 12 '11 at 8:40
@jalf - By removing the simple errors, the remaining ones are bigger on average. – Bo Persson Jul 12 '11 at 9:16

It generally means people are cheekily suggesting that C++ is even more awesome than C. Or that the extra pain that C++ introduces confers some special benefits.

C is very low level, will happily do whatever you tell it. It also has some strange peculiarities. C++ adds to this very complicated constructs, pedantic compiler warnings, and even more crufty and idiosyncratic syntaxes.

what's bad about pedantic compiler warnings? do you consider better typesafety an anti-feature? – phresnel Jul 12 '11 at 7:33
@phresnel: No, it's not "just type safety", C++ crams in a whole pile of half-baked paradigms as well. Type safety only gets you so far when you don't actually know if what the compiler is generating will do what you think it will. – Matt Joiner Jul 12 '11 at 23:18

At the time less was known about humans and their minds. There used to be an assumption, that the universe was made by a rational god, in a rational way, and a rational man could understand it, given time.

The combination of 2 tools, C++ and the machine it ran on, disproved that assumption. We can make a tool that we can not understand. The quote neatly creates a visual representation of the dangers of using such a tool.

Now we seek elegance, simplicity and beauty in code. We feel it is better.

However, I have just found out about Processing Fluency. Instantly I thought back to physics equations, and how a search for "elegance" was paramount.

Now, is truth, beauty, and beauty truth? Or is it a hack, and grotty solutions are just as valid?

We are wired to think the former, but the world we live in may not work that way. Where to go?

"We can make a tool we can not understand." Hear! Hear! I doubt if even the people who created C++ understand it properly, let alone a mere mortal like me. (+1) – ApprenticeHacker Jul 13 '11 at 8:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.