请看看这个无法解释的行为和memcpy(输出)重叠的内存块 [英] Please look into this inexplicable behavior and output of memcpy() for overlapping memory blocks

查看:181
本文介绍了请看看这个无法解释的行为和memcpy(输出)重叠的内存块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读下列关于的memcpy()之后,我继续阅读 memmove与()

After reading the following about memcpy(), I proceeded to read about memmove():

为了避免溢出,由目的地和源两个参数指向的数组,大小应至少NUM个字节,并且应该不重叠(重叠内存块,的memmove是一个更安全的方法) (LINK)

和检查用来说明 memmove与的工作()我决定用的memcpy(),而不是怎么看不同的是,output.To我吃惊的是,他们是在相同即使是重叠内存blocks.Here的情况下在程序和输出,我已经着手描述后,我的困惑:

And after checking the program used to illustrate the working of memmove() I decided to tweak it by using memcpy() instead to see how different is the output.To my surprise,they are the same even if it's a case of overlapping memory blocks.Here is the program and the output,and I have proceeded to describe my confusion after that:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "memmove can be very useful......";
  //memmove (str+20,str+15,11);
  memcpy(str+20,str+15,11);  //Simply used memcpy instead of memmove
  puts (str);
  return 0;
}

输出 memmove与可以是非常非常有用的。

这输出是一样的,因为它是 memmove与()。而这里是我的困惑:

This output is the same as it is for memmove().And here are my confusions:

1)为什么输出相同的两个?由于在的情况下,没有使用中间缓冲区的memcpy(),我希望复制通过复制字符开始STR + 15 位置到 STR + 20 位置(覆盖还有什么)字符 STR + 16 位置 STR + 21 位置,依此类推,直到str中的字符+ 20位置,其具有由现在改变为在STR + 15位置处的字符,被复制到STR + 25的位置。但它不是这样,没有覆盖,它的作用如同一个中间缓冲器被用来写的确切原string.Here的一个例证:

1) Why is output same for both?Since there is no intermediate buffer used in case of memcpy(),I expect the copy to start by copying the character in str+15 position to the str+20 position (overwriting what is there),character in str+16 position to str+21 position, and so on till character in str+20 position,which has by now changed to the character in str+15 position, to be copied to the str+25 position.But it's not so,there is no overwriting and it's acting as if an intermediate buffer is used to write the exact original string.Here's an illustration:

memmove can be very useful......  //Original positions before memcopy
               ^    ^
            str+15  str+20

memmove can be very vseful......
                    ^ copies str+15 to str+20

memmove can be very veeful......
                     ^ copies str+16 to str+21
memmove can be very verful......
                      ^ copies str+17 to str+22
memmove can be very veryul......
                       ^copies str+18 to str+23
memmove can be very very l......
                        ^ copies str+19 to str+24
memmove can be very very v......
                         ^ I expect 'v' to be copied from str+20 to str+25
                           as str+20  now has 'v',not 'u'
memmove can be very very ve.....
                          ^ I expect 'e' to be copied from str+21 to str+26 
                            as str+21 now has 'e' not 's'

那么,为什么是的memcpy()复制为 memmove与可以是非常非常有用,而不是 memmove与可以是非常非常非常v

2)立即从后援所产生的一个小问题,次要是下面说一下 memmove与()链接

2) Now a minor secondary question arising from it.The following is said about memmove() (LINK)

复制发生,如果中间缓冲液使用,使目标和源重叠。

到底是什么,如果在这里?是不是中间缓冲的真的用于 memmove与()

What exactly is as if here?Isn't an intermediate buffer really used for memmove()?

推荐答案

如果对象重叠,memcpy的行为是不确定的。有试图推理不确定的行为毫无意义。既然是未定义这违背原因。你知道规矩,他们清楚地记载。如果对象重叠,使用memmove与

If the objects overlap, the behaviour of memcpy is undefined. There's little point in trying to reason about undefined behaviour. Since it is undefined it defies reason. You know the rules, and they are clearly documented. If the objects overlap, use memmove.

至于使用好像,这是指定的行为的而不是放在实施的任何限制。这允许库实现者使用的任何方法,他们认为合适的,只要最终结果是一样的使用中间缓冲器。例如,实现可以检测到的对象不重叠,因此​​避免使用中间缓冲性能方面的原因。

As for the use of "as if", that is to specify behaviour but not place any limitations on implementation. This allows the library implementor to use whatever method they see fit, so long as the end result is the same as using an intermediate buffer. For example, the implementation could detect that the objects do not overlap and so avoid using an intermediate buffer for performance reasons.

这篇关于请看看这个无法解释的行为和memcpy(输出)重叠的内存块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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