好的移动物体? [英] Okay to move an object?

查看:66
本文介绍了好的移动物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



(在我开始之前,请不要建议我使用std :: vector而不是实际数组来使用

。)


我明白一个对象可以拥有资源(例如动态地分配内存),所以我们必须通过copy复制一个对象 -

构造而不是使用memcpy。


但是,可以手动移动对象(即使用memcpy或

memmove)?


我有一个旋转阵列的算法。它转过来:


ABCDEFG


进入:


BCDEFGA

如果第二次运行,那将成为:


CDEFGAB

我打算将它用于任何来自和数组char''s,一个

数组的向量'。


它移动物体的方式有什么问题吗?


这里有一些示例代码:

#include< iostream>

使用std :: cout;


#include< cstdlib>

使用std :: memcpy;


#include< cstring>

使用std :: memmove;

模板< class T>

void RotateArrayOnce(T * const p_start,T * const p_over)

{

unsigned long const len = p_over - p_start;


unsigned char * const p_temp_storage = new unsigned char [sizeof(T)

];


memcpy(p_temp_storage,p_start,si zeof(T));


memmove(p_start,p_start + 1,sizeof(T)*(len - 1));


memmove(p_over - 1,p_temp_storage,sizeof(T));


delete [] p_temp_storage;

}


int main()

{

char buffer [] =" ABCDE";


cout<<缓冲区<< ''\ n'';


RotateArrayOnce(缓冲区,*(&缓冲区+ 1) - 1);


cout< ;<缓冲区<< ''\ n'';


RotateArrayOnce(缓冲区,*(&缓冲区+ 1) - 1);


cout< ;<缓冲区<< ''\ n'';


std :: system(" PAUSE");

}

起初想,我不知道如何简单地在内存中移动一个

对象(假设目标位置适当对齐)。


只有我能看到哪些东西可能会让事情变得糟糕的是,如果对象

由于某种原因将其自己的地址保存在其中。

-


Frederick Gotham


(Before I begin, please don''t suggest to me to use "std::vector" rather
than actual arrays.)

I understand that an object can have resources (e.g. dynamically
allocated memory), and so we have to copy an object properly via copy-
construction rather than using memcpy.

However, is it okay to move an object manually (i.e. by using memcpy or
memmove)?

I have an algorithm for rotating an array. It turns:

A B C D E F G

into:

B C D E F G A

and if run a second time, that would become:

C D E F G A B
I intend for it to be used on anything from and array of char''s, to an
array of vector''s.

Is there anything wrong with the way it moves objects around?

Here''s some sample code:
#include <iostream>
using std::cout;

#include <cstdlib>
using std::memcpy;

#include <cstring>
using std::memmove;
template<class T>
void RotateArrayOnce( T * const p_start, T * const p_over )
{
unsigned long const len = p_over - p_start;

unsigned char * const p_temp_storage = new unsigned char[ sizeof(T)
];

memcpy( p_temp_storage, p_start, sizeof(T) );

memmove( p_start, p_start + 1, sizeof(T) * (len - 1) );

memmove( p_over - 1, p_temp_storage, sizeof(T) );

delete [] p_temp_storage;
}

int main()
{
char buffer[] = "ABCDE";

cout << buffer << ''\n'';

RotateArrayOnce(buffer, *(&buffer + 1) - 1 );

cout << buffer << ''\n'';

RotateArrayOnce(buffer, *(&buffer + 1) - 1 );

cout << buffer << ''\n'';

std::system("PAUSE");
}
At first thought, I don''t see how it could be a problem to simply move an
object in memory (assuming the target location is suitably aligned).

Only thing I can see which could make things go awry is if the object
kept its own address stored within it for some reason.
--

Frederick Gotham

推荐答案

Frederick Gotham写道:
Frederick Gotham wrote:
(在我开始之前) ,请不要建议我使用std :: vector而不是实际数组。)


std :: vector _are_实际数组。 ;-)

但是,可以手动移动对象(即使用memcpy或
memmove)吗?


这是间接的常见问题解答。

http://www.parashift.com/c++-faq-lit....html#faq-26.7

"什么是''POD类型''?"

我打算将它用于任何来自char'的数组,以及
矢量数组'。


向量PODs?

使用std :: memcpy;
(Before I begin, please don''t suggest to me to use "std::vector" rather
than actual arrays.)
std::vector _are_ actual arrays. ;-)
However, is it okay to move an object manually (i.e. by using memcpy or
memmove)?
That is, indirectly, a FAQ.

http://www.parashift.com/c++-faq-lit....html#faq-26.7
"What is a ''POD type''?"
I intend for it to be used on anything from and array of char''s, to an
array of vector''s.
Are vectors PODs?
using std::memcpy;




你可能会尝试一些std :: copy()系列的成员。它们是模板化的,所以它们会深入到你的实际类型并调用它的复制操作。


如果你坚持使用memcpy,你就是在执行相同的操作

reinterpret_cast< char *>(& element)在每个元素上。 ''reinterpret_cast<>''是
坏了,应该避免,包括所有类似的情况,秘密地将b
扔掉类型信息。

你还没有提出任何不使用std :: vector的情况,除此之外

已经遇到了你理解的函数。不要重新发明轮子,而是时钟上的



-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!



You might try some member of the std::copy() family. They are templated, so
they will drill down to your actual type and invoke its copy operations.

If you stick with memcpy, you are performing the equivalent of
reinterpret_cast<char *>(&element) on each element. ''reinterpret_cast<>'' is
bad, and should be avoided, including all similar situations that secretly
throw away type information.

And you have not yet made any case for not using std::vector, besides you
have stuck with functions that you understand. Don''t reinvent wheels while
on the clock!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Frederick Gotham写道:
Frederick Gotham wrote:
(在我开始之前,请不要建议我使用std ::矢量
而非实际数组。

我理解一个对象可以拥有资源(例如动态分配内存),因此我们必须通过复制正确复制一个对象 -
构造而不是使用memcpy。

然而,手动移动对象(即使用memcpy
或memmove)是否可以?


嗯?如果您理解我们必须正确复制对象,那么为什么要问它是否可以不正确地进行操作?

我有一个旋转算法数组。它转向:

ABCDEFG

进入:BCDEFGA

如果第二次运行,那将成为:

CDEFGAB

我打算将它用于任何来自char'的数组,以及
数组的向量'。br />
它移动物体的方式有什么问题吗?
(Before I begin, please don''t suggest to me to use "std::vector"
rather than actual arrays.)

I understand that an object can have resources (e.g. dynamically
allocated memory), and so we have to copy an object properly via copy-
construction rather than using memcpy.

However, is it okay to move an object manually (i.e. by using memcpy
or memmove)?
Huh? If you understand that we "have to copy an object properly",
why ask if it''s OK to do it improperly?

I have an algorithm for rotating an array. It turns:

A B C D E F G

into:

B C D E F G A

and if run a second time, that would become:

C D E F G A B
I intend for it to be used on anything from and array of char''s, to an
array of vector''s.

Is there anything wrong with the way it moves objects around?




是的。除非您的对象属于POD类型,否则使用''memcpy''或''memmove''

就是* undefined *。通常这意味着在不对语言实现施加太多限制的情况下定义

的结果是不可能的。


V

-

请在通过电子邮件回复时删除资金''A'

我不回复要回复最多,请不要问



Yes. Unless your objects are of a POD type, using ''memcpy'' or ''memmove''
on them is *undefined*. Usually it means that it''s impossible to define
the consequences of that action without imposing too many limitations on
the language implementations.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


文章< fw ****************** *@news.indigo.ie> ;,

Frederick Gotham< fg ******* @ SPAM.com>写道:
In article <fw*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.com> wrote:
(在我开始之前,请不要建议我使用std :: vector而不是实际数组。)
(Before I begin, please don''t suggest to me to use "std::vector" rather
than actual arrays.)




为什么?是否可以建议一种方法来完成你想要的东西

来实现?


template< typename FwIt>

void RotateArrayOnce(FwIt begin,FwIt end){

std :: rotate(begin,begin + 1,end);

}


除非你正在完成家庭作业,否则你就是在浪费时间。


如果你*正在做家庭作业,你需要做的是

使用std :: swap走下数组的第一个元素。



Why? Would it be OK to suggest a method of accomplishing what you want
to accomplish?

template < typename FwIt >
void RotateArrayOnce( FwIt begin, FwIt end ) {
std::rotate( begin, begin + 1, end );
}

Unless you are doing a homework assignment, you are wasting your time.

If you *are* doing a homework assignment, the what you need to do is
walk the first element of the array down using std::swap.


这篇关于好的移动物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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