Hopefully there is a simple answer to this as it seems a simple question, however I have not been able to find any information on this on the interwebs.
In the following code snippet, Visual Studio complains of unreachable code at the line “delete bytes;”
try
{
memcpy(bytes, other.bytes, count);
}
catch (...)
{
delete[] bytes;
throw;
}
Does memcpy not throw exceptions?
No. memcpy is a C function. It doesn’t know about C++ features such as exceptions. (Of course, it’s perfectly legal to use it in C++, although arguably not the best idea).
In response to karlphillip: I must indeed clarify my thoughts: in C++, memcpy should be used only for low-level buffer copies inside object private implementation. It shouldn’t be used as mundanely as it was in C (for example to copy numbers arrays) because plain-vanilla buffers are now usually hidden inside class implementations. Classes that wrap arrays or other large amount of data (such as std::array) expose methods to manipulate their contents. And by the time I write this, Mark Ransom nicely summarized it 😉