为什么std :: memcpy的行为对于不是TriviallyCopyable的对象是未定义的? [英] Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

查看:1193
本文介绍了为什么std :: memcpy的行为对于不是TriviallyCopyable的对象是未定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://en.cppreference.com/w/cpp/string/byte/memcpy


如果对象不是 TriviallyCopyable (例如标量,数组,C兼容结构),行为未定义。

If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined.

在我的工作中,我们使用 std :: memcpy 很长时间不使用TriviallyCopyable的位交换对象:

At my work, we have used std::memcpy for a long time to bitwise swap objects that are not TriviallyCopyable using:

void swapMemory(Entity* ePtr1, Entity* ePtr2)
{
   static const int size = sizeof(Entity); 
   char swapBuffer[size];

   memcpy(swapBuffer, ePtr1, size);
   memcpy(ePtr1, ePtr2, size);
   memcpy(ePtr2, swapBuffer, size);
}

,从未出现任何问题。

我理解,对非TriviallyCopyable对象滥用 std :: memcpy 并导致下游未定义的行为是微不足道的。但是,我的问题:

I understand that it is trivial to abuse std::memcpy with non-TriviallyCopyable objects and cause undefined behavior downstream. However, my question:

为什么 std :: memcpy TriviallyCopyable对象?为什么标准认为有必要指定?

Why would the behavior of std::memcpy itself be undefined when used with non-TriviallyCopyable objects? Why does the standard deem it necessary to specify that?

UPDATE

的内容http://en.cppreference.com/w/cpp/string/byte/memcpy已经修改响应此帖子和帖子的答案。当前说明:

The contents of http://en.cppreference.com/w/cpp/string/byte/memcpy have been modified in response to this post and the answers to the post. The current description says:


如果对象不是 TriviallyCopyable (例如标量,数组,C兼容结构),行为是未定义的,除非程序不依赖于目标对象的析构函数的影响(这不是由 memcpy ),目标对象的生命周期(结束,但不是由 memcpy 启动)

If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined unless the program does not depend on the effects of the destructor of the target object (which is not run by memcpy) and the lifetime of the target object (which is ended, but not started by memcpy) is started by some other means, such as placement-new.

PS

评论者@Cubbi:


@RSahu如果某些东西保证UB下游,会使整个程序未定义。但我同意,在这种情况下似乎可能围绕UB,并相应地修改cppreference。

@RSahu if something guarantees UB downstream, it renders the entire program undefined. But I agree that it appears to be possible to skirt around UB in this case and modified cppreference accordingly.


推荐答案

原来标准并没有说明 std :: memcpy 用于不是TriviallyCopyable的对象。 http://en.cppreference.com/w/cpp/string/的内容byte / memcpy 已被修改,以响应原始帖子和帖子的答案。当前描述为:

It turns out that the standard does not say anything about the behavior of std::memcpy for objects that are not TriviallyCopyable. The contents of http://en.cppreference.com/w/cpp/string/byte/memcpy have been modified in response to the original post and the answers to the post. The current description says:


如果对象不是TriviallyCopyable(例如标量,数组,C兼容结构),行为是未定义的,除非程序不依赖于目标对象(不是由memcpy运行)的析构函数的影响,并且目标对象的生命周期(结束,但不是由memcpy启动)通过一些其他手段开始,例如placement-new。

If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined unless the program does not depend on the effects of the destructor of the target object (which is not run by memcpy) and the lifetime of the target object (which is ended, but not started by memcpy) is started by some other means, such as placement-new.

这篇关于为什么std :: memcpy的行为对于不是TriviallyCopyable的对象是未定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆