6

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?

mgray88
  • 376
  • 1
  • 5
  • 10

4 Answers4

14

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

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • Is it just me or was memory management actually easier before such constructs as exceptions began to pop up and prevent deallocation? – John Aug 23 '11 at 16:08
  • What would you use in C++ to copy memory from one place to another? – karlphillip Aug 23 '11 at 16:08
  • 1
    @karlphillip, in C++ you try not to copy "memory" but copy objects instead; for that you use `std::copy`. – Mark Ransom Aug 23 '11 at 16:14
  • @John: manual resource management is certainly much harder in the presence of exceptions, which is why you should never do it. In C++, *all* dynamic resources should be managed by objects that release them in their destructor. Of course, some languages have exceptions but not deterministic destruction; use them at your peril. – Mike Seymour Aug 23 '11 at 16:26
  • @karlphillip I've seen quite a few situations when using `std::copy` to copy "memory" (that is, an array of char) was compiled into more efficient code than what was supplied in the C library's `memcpy()`. – Cubbi Aug 23 '11 at 16:55
  • 2
    I would love to see that assembly. I'll report back when I do. – karlphillip Aug 23 '11 at 17:04
  • People use C++ to write low-level code like device drives and embedded systems, and when they do, `memcpy` is a perfectly reasonable thing to call. – Brian Neal Aug 23 '11 at 19:08
5

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

No, memcpy does not throw exceptions.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
0

memcpy comes from C, which predates C++ exceptions, so no. You will never catch an exception that is thrown as a result of memcpy.

Daniel
  • 6,595
  • 9
  • 38
  • 70