1

I met such a C++ quiz: what would happen if a pointer is deleted twice?

  • A) it can abort the program
  • B) it can cause a failure
  • C) it can cause an error
  • D) it can cause a trap

and the answer is D.

I was a bit lost, what does "trap" mean? is it a special term in C++?

Codor
  • 17,447
  • 9
  • 29
  • 56
athos
  • 6,120
  • 5
  • 51
  • 95

3 Answers3

7

It doesn't mean anything in C++. It could mean an operating-system level error (such as a Posix signal), but without context I can't say.

The correct answer is that it causes undefined behaviour; but all the answers could be taken to be correct since undefined behaviour could cause any of these things to happen.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    We _can_ say. It is a well-established general computing term. This "anything can happen" cookie-cutter answer is well and good when we're language lawyering, but the guy is asking about what can happen in practice. In practice, "anything can happen" is simply not true. – Lightness Races in Orbit Jan 09 '15 at 12:13
  • 1
    @LightnessRacesinOrbit: Well, it's not well enough established for me to know what it specifically means in this context, so I at least can't say. Sorry for extrapolating my ignorance onto others, if there is a well-defined and universally accepted meaning of the word that I haven't come across. And I didn't say that literally anything could happen, just that any of these things could happen. – Mike Seymour Jan 09 '15 at 12:16
  • I quoted the definition of it in my answer. So my answer may help you. – Lightness Races in Orbit Jan 09 '15 at 12:17
  • @LightnessRacesinOrbit: Thanks. To quote the source of that Wikipedia page, "This term is associated with assembler programming [...] and appears to be fading into history among programmers as the role of assembler continues to shrink." So I'll stick to my opinion that it doesn't have a well-established meaning. – Mike Seymour Jan 09 '15 at 12:19
  • That's certainly your right. My experience suggests the contrary, but I can't persuade you of that. However it may help if you consider the meaning of the term "trap representation". – Lightness Races in Orbit Jan 09 '15 at 12:20
6

I shall simply directly quote the Wikipedia article on traps:

In computing and operating systems, a trap, also known as an exception or a fault, is typically[NB 1][1] a type of synchronous interrupt typically caused by an exceptional condition (e.g., breakpoint, division by zero, invalid memory access). A trap usually results in a switch to kernel mode, wherein the operating system performs some action before returning control to the originating process. A trap in a system process is more serious than a trap in a user process, and in some systems is fatal. In some usages, the term trap refers specifically to an interrupt intended to initiate a context switch to a monitor program or debugger.

This is highly generalised terminology and is not defined by C++, let alone specific to it. More crucially, you must pay attention to the "can" in the multiple choice answers, because there is no guarantee that anything will happen when you double-delete an object.

In fact, all four answers basically say the same thing.

The quiz seems confused anyway, since "deleting a pointer" is likely not what it means.

Not to be confused with SNMP traps:

In SNMP, a trap is a type of PDU used to report an alert or other asynchronous event about a managed subsystem.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Deleting the same memory which has been already deleted is undefined behavior. Anything could happen, although in my case, it gave a runtime error. Compiled in C++ using g++ 4.9.1

My program:

int main()
{
    int x = 5;
    int *ptr = &x;
    delete ptr;
    delete ptr;
}

It gave the following error:

*** Error in `./t': free(): invalid pointer: 0xbf971994 ***
shauryachats
  • 9,975
  • 4
  • 35
  • 48