使用memcpy时重叠的含义 [英] Meaning of overlapping when using memcpy

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

问题描述

我试图理解C库<string.h>

语法:void *memcpy(void*dst,const void*src,size_t n);

我知道此函数用于将指针src指向的内存内容复制到dst指针指向的位置,并返回dst指针指向的地址.

I know that this function is used to copy the contents of the memory pointed by pointer src to the location pointed by the dst pointer and return a address pointed by dst pointer.

我无法理解以下有关memcpy()的重要声明:

I am not able to understand the following important statement regarding memcpy():

  • 使用memcpy()时,内存地址不应重叠,如果重叠,则memcpy()是不确定的.
  • When using memcpy(), memory address should not overlap, if it overlaps then the memcpy() is undefined.

另一个查询是: 传递给函数第三个参数的值,即size_t n始终是整数值吗?

Another query is: Is the value passed to third argument of the function i.e size_t n is always an integer value?

推荐答案

根据评论,您的问题是您不了解重叠"是什么意思

From the comments your problem is that you don't understand what "overlapping" means:

重叠意味着:

这两个内存区域srcdst确实重叠:

Here the two memory regions src and dst do overlap:

但是在这里他们没有:

因此,如果您有重叠的内存区域,则不能使用memcpy,而必须使用memmove.

So if you have overlapping memory regions, then you cannot use memcpy but you have to use memmove.

第二个问题:

是的,size_t是无符号整数类型.第三个参数是要复制的字节数,因此除无符号整数类型外几乎没有其他内容.

Yes, size_t is an unsigned integer type. The third argument is the number of bytes to copy, so it can hardly be anything else than an unsigned integer type.

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

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