希望这是一个简单的答案,因为这似乎是一个简单的问题,但是我无法在互联网上找到任何与此有关的信息.
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.
在以下代码段中,Visual Studio抱怨删除字节;"行上无法访问的代码.
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; }memcpy是否不会引发异常?
Does memcpy not throw exceptions?
推荐答案否. memcpy是C函数.它不了解诸如异常之类的C ++功能. (当然,在C ++中使用它是完全合法的,尽管可以说不是最好的主意.)
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).
响应karlphillip:我确实必须澄清我的想法:在C ++中,memcpy应该仅用于对象私有实现内的低级缓冲区副本.不应像在C语言中那样平凡地使用它(例如,复制数字数组),因为普通香草缓冲区现在通常隐藏在类实现中.包装数组或其他大量数据的类(例如std :: array)公开用于操作其内容的方法.在我写这篇文章时,马克·兰瑟姆(Mark Ransom)很好地总结了它;-)
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 ;-)
更多推荐
memcpy是否不会引发异常?
发布评论