I made the core dumped error with the two pieces of codes below:
//test.cpp
int main()
{
int *p = new int;
*p = 100;
delete p;
delete p;
return 0;
}
//test2.cpp
int main()
{
int *p = new int;
*p = 100;
delete p;
*p = 111;
std::cout<<*p<<std::endl;
return 0;
}
Gdb told me that the first piece of code got core dumped because of the signal SIGABRT, whereas the second piece of code got core dumped because of the signal SIGSEGV.
Could you tell what is the difference?
The SIGABRT is explicitly detected and signaled by the implementation of delete whose detected the invalidity of the second delete. It is launch by calling the abort function
The SIGSEGV is different, it is undergoing rather than detected by a check in a library like the previous, it is launch through the memory management of the OS
see https://en.cppreference.com/w/c/program/SIG_types
Both of these examples are undefined behaviour, this means according to c++ the compiler (and system) can do whatever it wants.
SIGABRT
is signaled. SIGABRT
means abnormal termination condition, as is e.g. initiated by abort().SIGSEGV
signal. SIGSEGV
means invalid memory access (segmentation fault)
But both are still UB, so this is just a feature of your current compiler/os/system. The difference between the errors is clear from the definition of the errors here. One is an abort, usually generated by the compiler or coder. One is caused by invalid memory access, usually signaled by the operating system or hardware.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With