memcpy是否不会引发异常?

编程入门 行业动态 更新时间:2024-10-26 00:27:06
本文介绍了memcpy是否不会引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

希望这是一个简单的答案,因为这似乎是一个简单的问题,但是我无法在互联网上找到任何与此有关的信息.

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是否不会引发异常?

本文发布于:2023-10-31 05:12:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   memcpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!