Many people write that realloc does not give exceptions ... it also gives, here is the simplest example:
You see a bug in your code, but you decide that this is an exception. What actually happens.
// тут выделили память, все ок. char *a = (char*)malloc(10); // освободили память, тоже ок. Но free не меняет значение указателя. Он продолжает указывать на тот же кусок памяти. free(a); // а теперь пытаемся обратиться к этой памяти... a = (char*)realloc(a, 20);
What does realloc actually do? if it is greatly simplified, it allocates a new memory of the required size, copies the old memory to a new place, and frees the old memory. But any programmer knows that if you make free twice for a pointer, you will get an error. Approximate realloc code can be peeped and there are no obvious throws of exceptions.
So what's the "exception"? The debugger and compiler know that the programmer can make a mistake. And in the debug mode, add any checks. And if something is wrong, it intercepts immediately.
That is, you see an exception not from realloc, but from the debug memory manager and debugger, who intercepted the situation of double memory release (or maybe accessing the freed memory, but this is less likely) and scolding you.
DebugBreak();- nick_n_atry/catchnothing to do with any error trapping, and thereallocfunction does not throw exceptions. And, by the way, what does the [C] tag do here? - AnTtry/catchnever meant to catch unexpected drops in the program. And the exception in C ++ is precisely the situation when the code makes athrow.reallocdoesn't know anything aboutthrow, so it can't do it. The motley "departures" in the program have no relation to C ++ exceptions. (Read about SEH if you want, but this is nottry/catchand not C ++). - AnT