9

I see often the following terms in C++ interview questions :

  • program abort
  • error
  • failure
  • trap

I'm not sure to see clearly the differences between those terms. Can someone provide a clear concise explanation?

Edit : the context question was : "What happens when you delete a pointer twice?" but knowing the differences between those terms is more important for me than just the answer.

Laurent
  • 812
  • 5
  • 15
  • The interview questions will provide some context wich is clearly missing to give the consise explanation you are looking for. – Arne Mertz Aug 12 '13 at 10:11
  • 1
    This is far too generic. You need to supply an example question. It is, for instance, an error to `#define` a language keyword, but every compiler lets you do it without a word of warning. It can also be an error to pass an out of range parameter to a function, which may result in a program abort. – Tom Tanner Aug 12 '13 at 10:12
  • 1
    `What is a trap` -- see http://stackoverflow.com/questions/3149175/what-is-the-difference-between-trap-and-interrupt – devnull Aug 12 '13 at 10:13
  • For more on `trap` -- see http://www.cs.inf.ethz.ch/37-023/vorl/vorl16-01.tn.pdf and http://en.wikipedia.org/wiki/Trap_(computing) – devnull Aug 12 '13 at 10:15
  • I think "abort" is the only thing that is well-defined in terms of C++. Traps are a hardware implementation detail that C++ doesn't care about, and "error" and "failure" are... uh... blah blah that can mean _anything_. What error, what failure? Substitution failure? That's not an error, btw! (pun intended). Disk failure? Compile error? Program error? – Damon Aug 12 '13 at 10:18
  • @Damon traps are software interrupts, not hardware. – Marc Claesen Aug 12 '13 at 10:19
  • @MarcClaesen: Traps are "things" that happen on the hardware. If you divide by zero, the CPU will generate a trap. If your code has a trap instruction, it will generate a trap (obviously). If you access an invalid page, likewise. Sure, it is triggered by software, not by an external event. But whatever it is, it's an implementation detail of the _hardware_, it is absolutely not something that C++ cares about. You have no notion of traps in C++. – Damon Aug 12 '13 at 10:22
  • @Damon I agree that traps are generally not relevant for C++ programmers, but in some cases they *can* be. For example, a relatively new code obfuscation technique explicitly uses traps to prevent static disassembly. I guess this is probably entirely irrelevant for 99.9999% of all programs, though :-) Anyways, in this context any language can *use* traps for exotic purposes. http://static.usenix.org/event/sec07/tech/full_papers/popov/popov_html/ – Marc Claesen Aug 12 '13 at 10:28
  • I changed the title since the "possible duplicate" answer does not entirely answer my question – Laurent Aug 12 '13 at 10:32
  • About the "deleting a pointer twice" bit, the _likely_ thing to happen is a trap (usually the allocator does a bad memory access and just crashes!) but it might be something else too. Deleting a pointer that is not the null pointer twice is undefined behavior, so _aynthing_ could happen. – Damon Aug 12 '13 at 10:56

2 Answers2

4

These aren't really particular to C++.

  • Abort is when you terminate the program, or a particular operation, because of a problem. There is a C++ library function std::abort, inherited from the C library, which kills the program as if by an external signal, and does not run destructors or clean-up.

  • An error is when something goes wrong. In C++, many kinds of errors are not necessarily detected immediately. C++ instead specifies undefined behavior, which may involve quiet memory corruption that may cause mysterious misbehavior later.

  • A failure is when a program does the wrong thing. This is pretty generic engineering term. The pointy-haired boss is probably more familiar with this concept than the others, because it's the only one a customer is really aware of.

  • A trap is when the program detects an error condition and takes some action accordingly.

So if you detect that the network went down, and show a message to the user such as "Could not continue; your document has been automatically saved" before quitting, then you have trapped an error and aborted, but nevertheless there was a failure.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 2
    A trap is more often detected by the hardware (MMU, etc.). – James Kanze Aug 12 '13 at 10:54
  • @JamesKanze I think it's a pretty generic term. "I found a new kind of error that we need to trap" is something I would say in any software discipline. – Potatoswatter Aug 12 '13 at 10:59
  • Interesting. I've never heard it used this way; "I found a new kind of error that we need to catch" seems more likely to me. But my main thought is when the language speaks of a "trapping" representation; a bit pattern that causes a hardware trap (sending the program into a trap handler in the OS). – James Kanze Aug 12 '13 at 11:01
  • @JamesKanze In any case this is the same sense of "trap" as the terms "trapping representation," which represents a numerical error or "trap handler" which executes when hardware needs assistance with an exceptional condition. "Catch" and "trap" are pretty similar; if I recall you could even `catch` the execution of any trap handler in the old Forth-based Open Firmware OS. – Potatoswatter Aug 12 '13 at 11:08
0

All these terms are a bit vague, especially error and failure. They basically mean the same general thing: something's wrong. Program abort is probably referring to calling the C library standard abort function, which raises the SIGABRT signal. This usually results in an ungraceful program termination, but is dependant on platform and whether or not the signal is caught. Trap might refer to signal catching in general which is controlled by the C library standard signal function.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54