当按位交换()是一个坏主意的例子? [英] Examples of when a bitwise swap() is a bad idea?

查看:172
本文介绍了当按位交换()是一个坏主意的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你不应该来治疗对象指针的指针在OOP语言原始二进制数据,包括C ++。对象是比自己重新presentation。

You're not supposed to treat object pointers as pointers to raw binary data in OOP languages, including C++. Objects are "more than" their representation.

因此​​,例如,交换通过交换他们字节ING两个对象不正确:

So, for example, swaping two objects by swapping their bytes is incorrect:

template<class T>
void bad_swap(T &a, T &b)  // Assuming T is the most-derived type of the object
{
    char temp[sizeof(T)];
    memcpy(temp, &a, sizeof(a));
    memcpy(&a, &b, sizeof(b));
    memcpy(&b, temp, sizeof(temp));
}

唯一的局面,但是,在我能想象这个快捷方式造成的问题的是,当一个对象包含一个指向自己,这是我在实际中很少(从未?)可见;也有可能,但是,也有其它的方案。

The only situation, however, in which I can imagine this shortcut causing a problem is when an object contains a pointer to itself, which I have rarely (never?) seen in practice; there may, though, also be other scenarios.

什么是当一个正确的交换将如果执行按位交换打破一些实际的(真实世界)的例子吗?结果
我可以很容易拿出来与自我指针做作的例子,但我想不出任何真实世界的人的。

What are some actual (real-world) examples of when a correct swap would break if you performed a bitwise swap?
I can easily come up with contrived examples with self-pointers, but I can't think of any real-world ones.

推荐答案

我要认为这是几乎总是一个的除非在特殊情况不好的想法,其中分析已经完成,一交换的更加明显和清晰的实现了性能问题。即使在这种情况下,我只能用这种方法为直线上升没有继承结构去,从不为任何类型的类。你永远不知道什么时候继承将可能增加打破了整个事情(可能在真正阴险的方式太)。

I'm going to argue that this is almost always a bad idea except in the specific case where profiling has been done and a more obvious and clear implementation of swap has performance problems. Even in that case I would only go with this sort of approach for straight up no-inheritance structures, never for any sort of class. You never know when inheritance will be added potentially breaking the whole thing (possibly in truly insidious ways too).

如果你想有一个快速交换的实现也许是一个更好的选择(如适用)是平普尔类,然后就换​​出的实施(再次,这个假设没有分球给业主,但是这很容易包含对类和放大器; implement执行,而不是外部因素)

If you want a fast swap implementation perhaps a better choice (where appropriate) is to pimpl the class and then just swap out the implementation (again, this assumes that there are no back-pointers to the owner, but that's easily contained to the class & impl rather than external factors).

编辑:这种方法的可能的问题:

Possible problems with this approach:


  • 指针回自己(直接或间接)

  • 如果类包含其中一个直接的字节拷贝是没有意义的任何对象(递归有效这个定义),或者其复制通常是禁用

  • 如果该类需要任何形式的锁定复制

  • 这是容易的意外在两种不同类型的路过这里(所需要的是一个中间函数,隐式使派生类的样子父),然后你交换vptrs(哎哟!)

  • Pointers back to self (directly or indirectly)
  • If the class contains any object for which a straight byte-copy is meaningless (effectively recursing this definition) or for which copying is normally disabled
  • If the class needs any sort of locking to copy
  • It's easy to accidentally pass in two different types here (all it takes is one intermediate function to implicitly make a derived class look like the parent) and then you swap vptrs (OUCH!)

这篇关于当按位交换()是一个坏主意的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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