1

I have a basic question on segfaulting. The following pseudo code explains my question better. I have a pointer to an external api and on running it, segfault occurs. My question what happens next. Will the memory pointed to by the pointer api deleted? What does OS do after segfault happens?

int main () { 
    XAPI* api = new XAPI();

    // Assume: there is a segfault while in run()
    // What happens after segfault
    // does `delete api;` get executed?

    api->run();

    delete api;

    return 0;

}
Anand
  • 1,122
  • 3
  • 20
  • 33

1 Answers1

1

The default handling for SIGSEGV is to terminate and generate a core dump. The process is killed and its associated resources are freed.

Unless the program handles the signal, none of the following code will be executed.

See the signal(7) man page for more information.

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • @Hastrukun Just to be clear, since the associated resources are freed, memory pointed to api is cleaned up? – Anand Sep 12 '13 at 15:06
  • Generally, yes. memory is freed when the process terminates (see also [this question](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc)). Some things might persist (eg. SysV Semaphores), though you generally don't need to worry about those. – Hasturkun Sep 12 '13 at 15:07