我可以在C ++中使用memcpy来复制没有指针或虚函数的类 [英] Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

查看:453
本文介绍了我可以在C ++中使用memcpy来复制没有指针或虚函数的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个类,如下所示:

Say I have a class, something like the following;

class MyClass
{
public:
  MyClass();
  int a,b,c;
  double x,y,z;
};

#define  PageSize 1000000

MyClass Array1[PageSize],Array2[PageSize];

如果我的类没有指针或虚方法,可以安全使用以下? >

If my class has not pointers or virtual methods, is it safe to use the following?

memcpy(Array1,Array2,PageSize*sizeof(MyClass));

我问的原因是我正在处理非常大的分页数据集合, 这里,性能是至关重要的,memcpy提供了显着的性能优势,而不是迭代赋值。我怀疑它应该是好的,因为'this'指针是一个隐含的参数,而不是任何存储,但有任何其他隐藏的秘密,我应该注意吗?

The reason I ask, is that I'm dealing with very large collections of paged data, as decribed here, where performance is critical, and memcpy offers significant performance advantages over iterative assignment. I suspect it should be ok, as the 'this' pointer is an implicit parameter rather than anything stored, but are there any other hidden nasties I should be aware of?

编辑

根据sharptooths评论,数据不包含任何句柄或类似的引用信息。

As per sharptooths comments, the data does not include any handles or similar reference information.

根据Paul R的评论,我已经对代码进行了概要分析,避免了复制构造函数在这种情况下的速度快4.5倍。这里的部分原因是我的模板数组类比给定的简单示例有点复杂,并且为不允许浅复制的类型分配内存时调用一个布局new。这实际上意味着调用默认构造函数以及复制构造函数。

As per Paul R's comment, I've profiled the code, and avoiding the copy constructor is about 4.5 times faster in this case. Part of the reason here is that my templated array class is somewhat more complex than the simplistic example given, and calls a placement 'new' when allocating memory for types that don't allow shallow copying. This effectively means that the default constructor is called as well as the copy constructor.

第二次编辑

这也许值得指出,我完全接受,使用memcpy这种方式是不好的做法,应该在一般情况下避免。它被使用的特定情况是作为高性能模板数组类的一部分,其包括参数AllowShallowCopying,该参数将调用memcpy而不是复制构造函数。这对操作有很大的性能影响,例如删除数组开头附近的元素,以及将数据分入和分出二级存储。更好的理论解决方案是将类转换为一个简单的结构,但是这涉及到大量代码库的重构,避免它不是我渴望做的事情。

It is perhaps worth pointing out that I fully accept that use of memcpy in this way is bad practice and should be avoided in general cases. The specific case in which it is being used is as part of a high performance templated array class, which includes a parameter 'AllowShallowCopying', which will invoke memcpy rather than a copy constructor. This has big performance implications for operations such as removing an element near the start of an array, and paging data in and out of secondary storage. The better theoretical solution would be to convert the class to a simple structure, but given this involves a lot of refactoring of a large code base, avoiding it is not something I'm keen to do.

推荐答案

让我给你一个经验的答案:在我们的实时应用程序,我们一直这样做,它的工作很好。对于Wintel和PowerPC的MSVC,对于Linux和Mac的GCC,即使对于具有构造函数的类也是如此。

Let me give you an empirical answer: in our realtime app, we do this all the time, and it works just fine. This is the case in MSVC for Wintel and PowerPC and GCC for Linux and Mac, even for classes that have constructors.

我不能引用C ++标准的章节,只是实验证据。

I can't quote chapter and verse of the C++ standard for this, just experimental evidence.

这篇关于我可以在C ++中使用memcpy来复制没有指针或虚函数的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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