1

I have written the following program which should give runtime error of double free corruption.

#include <iostream>
#include <memory>
using namespace std;

int main()
{
    shared_ptr<int> shared3(new int);
    *shared3 = 9;
    int *raw = shared3.get();
    delete raw;

    return 0;
}

A core dump should have come but it didn't. Please tell me how can I get double free runtime error in my environment?

  • 4
    Undefined Behaviour includes appearing to work. There is no way to guarantee that the Runtime Support or OS will detect any Undefined Behaviour. – Richard Critten May 29 '22 at 19:02
  • Well this is what I got on Ubuntu 22.04: `free(): double free detected in tcache 2 Aborted (core dumped) `. Hopefully it's now clear to you that it depends on the system whether your program crashes or not. – digito_evo May 29 '22 at 19:14
  • Think of code as a description of program behaviour, not a list of instructions to be slavishly followed. The compiler will take the description and produce a, preferably optimal, list of instructions that will produce the behaviour. If the described behaviour is undefined, the instructions produced will be undefined. – user4581301 May 29 '22 at 19:16
  • 1
    *A core dump should have come but it didn't.* This assertion is false. – Eljay May 29 '22 at 19:28

0 Answers0