Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does memcpy not throw exceptions?

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?

like image 878
mgray88 Avatar asked Aug 23 '11 16:08

mgray88


People also ask

Can memcpy throw exception?

memcpy is a pure C function (so it does not throw C++ exceptions).

Does memcpy clear memory?

memcpy() itself doesn't do any memory allocations. You delete what you new , and delete[] what you new[] . You do neither new nor new[] . Both source and destination arrays are allocated on the stack and will be automatically deallocated when then go out of scope.

What can I use instead of memcpy?

memmove() is similar to memcpy() as it also copies data from a source to destination.

What does memcpy do?

The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string.


2 Answers

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 ;-)

like image 184
Serge Wautier Avatar answered Sep 21 '22 12:09

Serge Wautier


Your catch block catches C++ exceptions. On your platform, memcpy is not defined to throw C++ exceptions, and your compiler knows it, so it correctly informs you that your catch block will never execute.

It's allowed for memcpy to throw C++ exceptions. The function's behavior is undefined for cases when either of the pointers doesn't point at a valid object. It's perfectly valid for the function to throw a C++ exception in that situation because undefined behavior means it's perfectly valid for it to do anything.

You might find that memcpy on your platform throws OS exceptions when you're reading or writing invalid memory. You could get an access violation, but the C++ catch block doesn't catch that kind of exception. Use __try and __except for that. Better yet, analyze and edit your program to make sure you never get into such a situation in the first place. Then you don't have to worry about how any particular platform behaves.

like image 35
Rob Kennedy Avatar answered Sep 22 '22 12:09

Rob Kennedy