使用memcpy复制类 [英] using memcpy to copy classes

查看:70
本文介绍了使用memcpy复制类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用memcpy将一个类复制到同一类型的另一个类。

有一些原因我为什么不得不做一些奇怪的崩溃和

也许我正在做一些狡猾的复制这样的课程?类

不包含动态分配的数据,只包含标准类型和数组。这里

就是我正在做的一个例子,我知道它可能看起来很奇怪。我是

假设这将复制所有数据,但不能确定工作情况和

这样做的含义:


o * c1 = new o();

o * c2 = new o();


// c1和c2上的各种内容...

//在c1上复制c2并删除c2

memcpy(c1,c2,sizeof(* c1));


删除c2;


//继续使用c1(这是删除的c2的副本)

这样可以吗?


干杯,spoc

I have been using memcpy to copy one class to another of the same type.
There are reasons why I had to do this bug am getting some odd crashes and
maybe I''m doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here
is an example of what I''m doing, I know in isolation it may seem odd. I am
presuming this will copy all data ok but not really sure of the workings and
implications of doing it this way:

o* c1 = new o();
o* c2 = new o();

// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));

delete c2;

// continue working with c1 (which is a copy of the deleted c2)
Is this ok?

Cheers, spoc

推荐答案

2004年8月31日星期二15:46:38 GMT,spoc < SP ** @ enterprise.com>写道:
On Tue, 31 Aug 2004 15:46:38 GMT, "spoc" <sp**@enterprise.com> wrote:
我一直在使用memcpy将一个类复制到同一类型的另一个类。
有一些原因我为什么不得不做一些奇怪的崩溃和
也许我正在做一些像这样狡猾的复制课程?这些类
不包含动态分配的数据,只包含标准类型和数组。这里
是我正在做的一个例子,我知道它可能看起来很奇怪。我是
假设这将复制所有数据,但不能确定工作方式和这样做的含义:

o * c1 = new o();
o * c2 = new o();

// c1和c2上的各种内容...

//在c1上复制c2并删除c2 < brc> memcpy(c1,c2,sizeof(* c1));

删除c2;

//继续使用c1(这是已删除的c2的副本) )

这样可以吗?

干杯,spoc
I have been using memcpy to copy one class to another of the same type.
There are reasons why I had to do this bug am getting some odd crashes and
maybe I''m doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here
is an example of what I''m doing, I know in isolation it may seem odd. I am
presuming this will copy all data ok but not really sure of the workings and
implications of doing it this way:

o* c1 = new o();
o* c2 = new o();

// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));

delete c2;

// continue working with c1 (which is a copy of the deleted c2)
Is this ok?

Cheers, spoc




(这是常见问题吗??)


使用哑复制复制类对象总是一个不好的想法

语义(即逐位复制),因为你可能正在复制

指向已分配内存的指针在删除第一个要删除的第一个类时被删除;通过其他类访问它们将导致未定义的行为导致
。在不知道

类的确切布局的情况下,很难知道。


类通常比结构更复杂,因为它

将实现自己的复制语义。此外,一个类可以通过声明其副本

构造函数和/或赋值运算符私有来禁止复制(即复制正常方式)。在我的第一个paragaph中,

你可以复制指针(浅拷贝),但你正在分享他们指向的内存

;通常,该类将安排做一个深度的b
副本,分配额外的存储空间并复制

指针所引用的所有数据,或者 - 更复杂 - - 实施

某种引用计数。


即使类声明看起来相对无害,它也可能有一些成员

它们本身就是类(例如std :: string,

std :: list等)。


IOW,不要这样做!


-

Bob Hairgrove
没有********** @ Home.com


spoc写道:
我一直在使用memcpy将一个类复制到同一类型的另一个类。
有一些原因我为什么不得不做一些奇怪的崩溃并且
也许我正在做一些狡猾的复制类这样的课程?这些类
不包含动态分配的数据,只包含标准类型和数组。这里
是我正在做的一个例子,我知道它可能看起来很奇怪。我是
假设这将复制所有数据,但不能确定工作方式和这样做的含义:

o * c1 = new o();
o * c2 = new o();

// c1和c2上的各种内容...

//在c1上复制c2并删除c2 < brc> memcpy(c1,c2,sizeof(* c1));

删除c2;

//继续使用c1(这是已删除的c2的副本) )

这样可以吗?

干杯,spoc
I have been using memcpy to copy one class to another of the same type.
There are reasons why I had to do this bug am getting some odd crashes and
maybe I''m doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here
is an example of what I''m doing, I know in isolation it may seem odd. I am
presuming this will copy all data ok but not really sure of the workings and
implications of doing it this way:

o* c1 = new o();
o* c2 = new o();

// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));

delete c2;

// continue working with c1 (which is a copy of the deleted c2)
Is this ok?

Cheers, spoc




是什么定义的o?您认为

标准类型是什么?如果o包含字符串,向量或任何非原始的
类型,则存在未定义行为的风险,因此崩溃因为

字符串,向量等在内部分配内存。


如果''o''只包含浮点数,字符,整数和固定大小的数组

,那么memcpy应该可以工作,我相信。指针可以复制,只需

不要用它们来管理记忆(正如你所说的那样)


o''有虚拟功能还是多个基础?


而且,你能不能告诉我们为什么你_just_使用memcpy?虽然我认为还有其他原因,但我只能想到一个原因(速度)



-mat



What is the definition of the class o? And what do you consider
"standard types". If o contains strings, vectors, or any non-primitive
type, you run the risk of undefined behavior, and hence crashes since
strings, vectors, etc allocate memory internally.

If ''o'' consists only of floats, chars, ints, and fixed-size arrays of
such, then memcpy should work, I believe. Pointers are OK to copy, just
don''t use them to manage memory (as you say you don''t)

Does class ''o'' have virtual functions or multiple bases?

And, could you fill us in on why you _must_ use memcpy? I can only think
of one reason (speed) though I imagine there are others.

-matt



" Matthew Hall" <毫安**** @ math.uiuc.edu>在消息中写道

news:ch ********* @ news.ks.uiuc.edu ...

"Matthew Hall" <ma****@math.uiuc.edu> wrote in message
news:ch*********@news.ks.uiuc.edu...
spoc写道:
我一直在使用memcpy将一个类复制到同一类型的另一个类。
有一些原因我为什么不得不做一些奇怪的崩溃
并且我正在做某事像这样狡猾的复制课程?这些类
不包含动态分配的数据,只包含标准类型和数组。
这是我正在做的一个例子,我知道它可能看起来很奇怪。我假设这将复制所有数据,但不能确定工作量
以及这样做的含义:

o * c1 = new o();
o * c2 = new o();

// c1和c2上的各种内容...

//在c1上复制c2并删除c2 < brc> memcpy(c1,c2,sizeof(* c1));

删除c2;

//继续使用c1(这是已删除的c2的副本) )

这样可以吗?

干杯,spoc
I have been using memcpy to copy one class to another of the same type.
There are reasons why I had to do this bug am getting some odd crashes and maybe I''m doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I''m doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and implications of doing it this way:

o* c1 = new o();
o* c2 = new o();

// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));

delete c2;

// continue working with c1 (which is a copy of the deleted c2)
Is this ok?

Cheers, spoc



类o的定义是什么?您认为
标准类型是什么?如果o包含字符串,向量或任何非原始类型,则存在未定义行为的风险,因此崩溃,因为
字符串,向量等在内部分配内存。
如果''o''只包含浮点数,字符,整数和固定大小的数组,那么memcpy就可以了,我相信。指针可以复制,只是不用它们来管理记忆(正如你所说的那样)

类o是否有虚函数或多个基地?

并且,您能否填写我们为什么_must_使用memcpy?我只能想到一个原因(速度)虽然我想还有其他原因。
-matt



What is the definition of the class o? And what do you consider
"standard types". If o contains strings, vectors, or any non-primitive
type, you run the risk of undefined behavior, and hence crashes since
strings, vectors, etc allocate memory internally.

If ''o'' consists only of floats, chars, ints, and fixed-size arrays of
such, then memcpy should work, I believe. Pointers are OK to copy, just
don''t use them to manage memory (as you say you don''t)

Does class ''o'' have virtual functions or multiple bases?

And, could you fill us in on why you _must_ use memcpy? I can only think
of one reason (speed) though I imagine there are others.

-matt



嗨Mat


我只使用字符,整数和固定数组。该类只有一个基础和

DOES包含一些虚函数。说实话,我正在使用memcpy而不是
了解类和它的基础是如何组织在内存和

虚拟函数等等所以我猜它这是一个糟糕的主意。


我这样做的原因是因为我正在攻击我的一些旧代码而且我是一个b $ b匆匆忙忙。


干杯,


Spoc


Hi Mat

I''m using chars, ints and fixed arrays only. The class has just one base and
DOES contain a few virtual functions. To be honest I''m using memcpy without
understanding how the class and it''s base is organised in memory and the
virtual functions etc. So I guess it''s a bad idea.

The reason I''m doing this is because I''m hacking some of my old code and am
in a bit of a rush.

Cheers,

Spoc


这篇关于使用memcpy复制类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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