Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jul 12 '11 at 7:17

This question came from our site for professional and enthusiast programmers.

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

7 Answers 7

up vote 80 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.

share|improve this answer
1  
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
1  
@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
2  
@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
    
As an example, I once had the dubious "honor" of inheriting another programmer's work on a cash register system and one of its myriad bugs was that it constantly printed empty invoices no matter what you purchased. Eventually I tracked the fault down to an unintialized variable that was being used as the index in a while i < total loop -- causing the randomly allocated value to almost always exceed the number of items in the invoice... –  Shadur Jul 12 '11 at 17:26
2  
@Shadur: Was he unaware of the existence of for loops? –  JAB Jul 12 '11 at 20:40

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?

share|improve this answer
    
"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

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: http://www2.research.att.com/~bs/bs_faq.html#really-say-that

share|improve this answer

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.

share|improve this answer
    
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
5  
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
4  
@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.

share|improve this answer
2  
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

In C the programmer has a lot of control and flexibility, which allows also to make very painful mistakes. Like shooting yourself in the foot with a rope.

C++ fixes some of the C problems (replacing the rope with the bazooka), but is still flexible enough to make you able to hurt yourself, and is much more powerful.

share|improve this answer
    
Your simile is rather interesting. (Shooting yourself in the foot with a rope?) –  Mehrdad Jul 12 '11 at 7:18
2  
hey, don't shoot the messanger, at least not with a rope please! :-D –  littleadv Jul 12 '11 at 7:19
    
Oh the punniness is killing me... –  Mehrdad Jul 12 '11 at 7:19
    
hey man put that rope down we dont want any trouble –  Jesus Ramos Jul 12 '11 at 7:20
    
@Jesus: Would a gun be better? lol.. –  Mehrdad Jul 12 '11 at 7:20

This means that C++ is much more complex than C and opens many more ways for misuse with dire consequences.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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