-1

Here I have tried to catch the write access violation, but it doesn't work. I have tried some other ways but none worked for me. Can you please explain how it should work?

int main(){
    int *pint = new int();

    delete pint;

    try
    {
        *pint = 100;
    }
    catch (...)
    {
        cout << "this is the one " << endl;
    }
}
hering
  • 1,956
  • 4
  • 28
  • 43
Hara
  • 35
  • 4
  • 5
    This "use after free" detonates undefined behavior. It does not cause C++ exception to be thrown. So there is nothing to catch. – user7860670 Apr 20 '17 at 09:55
  • There is no such thing as a "writing access exception" in C++. – David Schwartz Apr 20 '17 at 19:56
  • 2
    The only way to survive shooting yourself in the head, is to not get shot in the head in the first place. Because once the bullet penetrates the cranium and exits on the opposite side, it will have irreversibly disrupted enough neural matter that it's not possible to restore it to a usable state. Therefore, you must not get shot in the head. – Mysticial Apr 20 '17 at 22:10

2 Answers2

1

Memory access violations have undefined behaviour - as such, they are not specified to throw. It is not possible to catch something that wasn't thrown.

There is no way to detect whether a memory violation (or anything else that has undefined behaviour) has occurred within standard C++ - although there are external tools that can be used to debug your program: http://valgrind.org/

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

C++ doesn't throw runtime exceptions when you do something illegal (read: something that used undefined behavior). That's only a Java thing. In C++, that doesn't happen.

When you write *pint = 100;, you are assigning 100 to memory you do not own anymore, and one possible result is that the kernel will send a SIGSEGV to the application, effectively killing it, because it accessed memory it doesn't own. There are ways to catch that signal though, so you could use that, but that is not recommended - you rarely have a real reason to.

But you can't use try-catch, because no exception is thrown whatsoever. The program could also continue executing, you just have no guarantees.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • Thank you .. can you please guide me about how to handle this. how can i catch such situations. i am programming on windows – Hara Apr 20 '17 at 10:00
  • @Hara You should use `Application Verifier` available as part of Windows SDK, it can catch many kinds of memory use problems. – user7860670 Apr 20 '17 at 10:06
  • @user2079303 True, fixed. Thanks :) – Rakete1111 Apr 20 '17 at 10:14
  • @Rakete1111 Nice :) Another nit: While handling a SIGSEGV is theoretically possible, I wouldn't suggest using it. It's in the "here be dragons" territory and could lead beginners into a trap. – eerorika Apr 20 '17 at 10:17
  • 3
    @Hara - The way to handle this is *avoiding* the problem. If you don't use `new` and `delete` with raw pointers it never ever happens. – Bo Persson Apr 20 '17 at 10:17
  • @Hara To elaborate on what Bo suggests, please learn how to use the [RAII pattern](http://stackoverflow.com/questions/712639/understanding-the-meaning-of-the-term-and-the-concept-raii-resource-acquisiti#713773) – Aaron3468 Apr 20 '17 at 22:04