关于memmove的问题 [英] Questions about memmove

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

问题描述

我对''memmove''的几个理论问题:


1.是否有充分的理由使用memcpy而不是memmove?

2. memmove对复制结构是否有用(而不是=运算符)?

3.一般情况下,什么时候应该明确地使用memmove? [我只看到它在代码中用于复制char数组的
,即便如此,< string.h>提供了大部分

必要的功能。]

Just a few theoretical questions I had on ''memmove'':

1. Is there ever a good reason to use memcpy instead of memmove?
2. Is memmove useful to copy structs (as opposed to = operator)?
3. In general, when should memmove explicitly be used? [I have only seen it
used in code to copy char arrays and even then, <string.h> provides most of
the necessary functionality.]

推荐答案

Method Man写道:
Method Man wrote:
Just我对''memmove''的几个理论问题:

1.是否有充分的理由使用memcpy而不是memmove?
是的,当源和目标不重叠时。

memcpy函数可能更有效,因为它没有像b $ b那样小心memmove。


2. memmove对复制结构是否有用(而不是=运算符)?
只要结构不包含指针或

结构的实例(可能包含指针)。有了指针,一个

就会遇到所有权和重复问题。


3.一般情况下,何时应该明确地使用memmove? [我只看到它在代码中用于复制char数组,即便如此,< string.h>提供了大部分必要的功能。]
Just a few theoretical questions I had on ''memmove'':

1. Is there ever a good reason to use memcpy instead of memmove? Yes, when the source and destination do not overlap.
The memcpy function may be more efficient since it doesn''t have
to be as careful as memmove.

2. Is memmove useful to copy structs (as opposed to = operator)? As long as the structs do not contain pointers or instances of
structures (which may contain pointers). With pointers, one
runs into ownership and duplication issues.

3. In general, when should memmove explicitly be used? [I have only seen it
used in code to copy char arrays and even then, <string.h> provides most of
the necessary functionality.]



当源和

目的地位置重叠时,应该明确使用memmove函数。


-

托马斯马修斯


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++- faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/ <其他网站:
http:// www。 josuttis.com - C ++ STL图书馆书籍
http://www.sgi。 com / tech / stl - 标准模板库


The function memmove should explicity be used when the source and
destination locations overlap.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library




" Method Man" < a@b.c>写了

"Method Man" <a@b.c> wrote

1.有没有充分的理由使用memcpy而不是memmove?

是的,如果memmove()保留给你,它会使你的意图更清晰

可能重叠的区域。另外调用

memcpy()可能更有效。
2. memmove对复制结构是否有用(而不是=运算符)?

使用memcpy () 为了这。我个人不喜欢应用于struct

赋值的=运算符,因为它无法明确这可能是一个昂贵的

操作。对于习惯于使用其他语言的人来说,它也无法成功地显示出这是一个浅层副本。
3.一般来说,何时应该明确地使用memmove? [我只看到它在代码中用于复制char数组的
,即便如此,< string.h>提供了大部分
的必要功能。]

1. Is there ever a good reason to use memcpy instead of memmove?
Yes, it makes your intentions clearer if memmove() is reserved for
potentially overlapping regions. Also it may be more efficient to call
memcpy().
2. Is memmove useful to copy structs (as opposed to = operator)?
Use memcpy() for this. Personally I dislike the = operator applied to struct
assignments, since it fails to make clear that this may be an expensive
operation. To people used to other languages, it also fails to make it
obvious that this is a "shallow copy".
3. In general, when should memmove explicitly be used? [I have only seen it used in code to copy char arrays and even then, <string.h> provides most of the necessary functionality.]



假设我们有一个对象数组,我们想在
$中插入一个项目b $ b中间。最后的项目未使用,因为阵列容量比实际内容大
。通过调用memove我们可以将所有项目向上移动

,然后插入我们的条目。

由于此操作很昂贵,所以不经常使用它(如果你

期望很多插入,你会使用树或链表代替)。但

它仍然有用。


Let''s say we have an array of objects, and we want to insert an item in the
middle. The items at the end are unused, because the array capacity is
bigger than the actual contents. By calling memove we can move all items up
by one, and then insert our entry.
Since this operation is expensive, it is not used all that often (if you
expect a lot of inserts, you would use a tree or a linked list instead). But
it is still useful on occasions.


Method Man写道:
Method Man wrote:

只是几个关于memmove的理论问题:

1.是否有充分的理由使用memcpy而不是memmove?


如果你在非重叠的内存区域之间进行复制。

2. memmove对复制结构是否有用(而不是=运算符)?


我更喜欢赋值运算符进行结构复制。

它为编译器提供了完成契约的机会

无论哪种方式最好。

如果你的结构只有几个int类型成员,

那么一个结构赋值,

可能只转化为几个int赋值说明。

3.一般情况下,何时应该明确地使用memmove?
[我只看到它在代码中用于复制char数组,即便如此,
< string .H>提供了大部分必要的功能。]

Just a few theoretical questions I had on ''memmove'':

1. Is there ever a good reason to use memcpy instead of memmove?
If you''re copying between nonoverlapping regions of memory.
2. Is memmove useful to copy structs (as opposed to = operator)?
I prefer the assignment operator for stucture copying.
It gives the compiler an oportunity to accomplish the deed
whichever way is best.
If your structure has just a few int type members,
then a structure assignment,
might translate into just a few int assignment instructions.
3. In general, when should memmove explicitly be used?
[I have only seen it used in code to copy char arrays and even then,
<string.h> provides most of the necessary functionality.]




memmove是一个字符串函数。你是什​​么意思

"< string.h>提供了大部分必要的功能。?


-

pete



memmove is a string function. What do you mean by
"<string.h> provides most of the necessary functionality."?

--
pete


这篇关于关于memmove的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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