What happens when a pointer is deleted twice?

Options
- It can abort the program
- It can cause a failure
- It can cause an error
- It can cause a trap


CORRECT ANSWER : It can cause a trap

Discussion Board
no.

It leads to UB. "trap" is not a c++ term.

aaa 05-26-2018 08:48 AM

Double correcption

If the pointer is allocated dynamically, Then, Mostly , It throws "Double time correction" and it crashes the program(exe).

Gunasekar 02-21-2017 11:28 PM

Trap is not a meaning full some what explaination is correct

in general if we deletes the pointer double time nothing will happen, depends on compiler also

if we delete null pointer also nothing will happen y because null pointer is a value..

Ali 02-11-2017 05:50 AM

what they mean by trap

could be a Dead-Lock of the program flow ;) Not necessarily caused by 2 threads blocking each other, but by endless loop, from which a thread does not exit. In that case the answer "Trap" is wrong :)
.
Some compilers even allow to automatically set deleted pointer to NULL, but it is anyway a good practice to set it to NULL by hand. In that case, a segmentation fault (meaning program counter register points to a mem. location outside region allocated by OS to my program.)
.
If was not set to NULL, it could also - by chance in the time of 2nd deletion - point to a mem. location within my programs page (allocated by OS). In that case the 2nd delete would most probably delete some needed data (or even not needed - depends on program contex). So the behaviour and possible program damage is highly dependent on progam state, random circumstances, perhaps on compiler, whether the pointer after first delete was set to NULL - simply on many random assumptions.
.
Therefore, this question is not quite proper and appropriate to ask in such simplified way.
I definitely disagree with suggested answer "a trap" - even if TRAP means a single thread Dead-Lock (looped state of a thread, from which it never exits).

Peter 10-5-2016 06:35 AM

undefined behaviour

Refering to a deallocated memory region causes undefined behaviour. This means that pretty much anything can happen from appearing to work to create yellow elephants. So E.S.'s answer is totally right.
I think they should have accpeted all the answers, because "anithing can happen" means that it can abort the program, it can cause a failure, ...
If I am wrong, please tell me.
I don't have any clue what does trap mean. Please explain it.

Daniel Sandor 06-20-2015 10:38 AM

kindly modify

plz give the details of trap.. as this term is not verifed in any of the c and c++ book....

MOHITA 05-19-2015 11:01 PM

They called it "trap"

Trap is not a c++ term, it is term of *some* OSes. OS can be different...
After pointer was deleted it is still pointing to same memory address.
Use of that pointer can cause undefined behavior.
Normally it will cause the memory access violation and call will be "trapped".
In some cases, if object memory was not overwriten and allocated for another object (so memory location is still valid) it would look like object "worked" properly without any "trap".

So proper answer is: All FOUR answers: it will be TRAPped with ERROR and will cause the ABORT of program with FAILURE...

E.S. 04-7-2015 06:07 PM

trap

explain trap


rohini 01-12-2015 12:23 AM

I disagree!

In multithreaded C++, the first deallocation frees the memory, before the next another thread could allocate memory, and thus, your next delete, deletes memory in error.
Your "trap" explaination, is something I have never heard of before.

norm 10-23-2014 02:38 PM

This answer is wrong

http://en.cppreference.com/w/cpp/memory/new/operator_delete
"In all cases, if ptr is a null pointer, the standard library deallocation functions do nothing."

I don't know what you mean by "can" and what compiler you are using for this but I am going to say that this question is probably wrong. The answer is nothing happens.

Michael Brune 10-5-2014 02:23 PM

what is trap.(what will happen when a pointer is deleted twice

Kindly detail about trap

srinivas 08-22-2014 10:02 PM

c++0

good question

monika 07-27-2014 03:23 PM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement