memmove 不动
memmove 并没有真正移动内存,不是吗?它只是将内存从一个区域复制到另一个区域,并允许这两个区域重叠。我问这个问题是因为我只是想知道为什么这个 fnc 会以一种非常误导性的方式被调用。
因为我知道,当某物从一个地方移动到另一个地方时,“东西”是在另一个地方进行此操作之后,而不是在第一个地方。而使用 memmove 则不会这样。我说得对吗?
memmove doesn't really move memory isn't that right? It just copies memory from one region to other and allows those two regions to overlap. I'm asking this question because I just want to know why is this fnc called in very misleading manner.
For I understand that when something is moved from one place to the other, the "thingy" is after this operation in the other place and not in the first. And with memmove it doesn't work that way. Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你说得对,它复制了它。不过,
memmove
和memcpy
之间是有区别的,因为memmove
可以正确处理缓冲区重叠的情况,所以推荐在这些中使用案例。但是,由于
memmove
执行额外的检查,当缓冲区较小且肯定不会重叠时,memcpy
更好。You are right, it copies it. However, there is a difference between
memmove
andmemcpy
, becausememmove
can treat correctly the case when the buffers overlap, so it is recomended in these cases.However, because of additional checks that
memmove
performs, when the buffers are small and surely does not overlap,memcpy
is better.memcpy()
和memmove()
之间的区别在于,memmove()
始终可以安全使用,无论源和目标之间存在别名如何。这就好像memmove()
首先将数据复制到临时缓冲区,然后再复制到目的地。memcpy()
不提供任何别名保证。它可能按预期工作,但也可能不按预期工作。如果您知道缓冲区不能重叠,memcpy()
就可以,并且可以在任何给定的库中使用优化,使其比memmove()
更快。在另一个库中,memcpy()
实际上可能只是memmove()
。如果您知道 src 和 dst 不能是别名,则可以安全地使用
memcpy()
。如果您不知道哪一个适用于您,请使用memmove()
。The difference between
memcpy()
andmemmove()
is thatmemmove()
is always safe to use, regardless of aliasing between source and destination. It is as thoughmemmove()
copies data to a temporary buffer first and then to the destination.memcpy()
does not offer any aliasing guarantees. it may work as intended, but maybe not. If you know buffers cannot overlap,memcpy()
is fine, and may in any given library use optimizations that allow it to be faster thanmemmove()
. In another librarymemcpy()
may actually just bememmove()
.If you know src and dst cannot be aliases, it is safe to use
memcpy()
. If you don't know which one applies to you, usememmove()
.该函数之所以如此命名,是因为如果被复制的内存区域do碰巧重叠,则它不再是副本,因为原始缓冲区不再不变。因此,原始缓冲区应被视为不可用。由于您使用的是 memmove 而不是 memcpy,情况可能就是这样。因此,命名是有意义的:从语义上讲,您正在移动数据而不是复制数据。
The function is named as such because if the memory regions being copied do happen to overlap it is no longer a copy, since the original buffer is no longer unchanged. Therefore the original buffer should be considered unusable. Since you're using memmove not memcpy this is likely to be the case. Therefore the naming makes sense: semantically you're moving the data not copying it.
Memmove 将数据从源复制到目标。它与 memcpy 的不同之处在于它保证在重叠的内存区域上工作。
Memmove copies data from the source to the destination. It differs from memcpy in that it is guaranteed to work on overlapping memory regions.
是的,
memmove
确实是memcpy
,具有处理重叠块的能力。假设您有一个数组,并且您想在开头插入一些新项目(或删除一些项目),那么从语义上讲,您正在做的就是来回“移动”(现有或剩余)项目 - 我猜,并且在这种情况下,这个名字就有意义了。Yes,
memmove
is reallymemcpy
with the ability to handle overlapping blocks. Let's say you have an array, and you want to insert some new items at the begining (or remove some items), then symantically what you are doing is "moving" the (existing or remaining) items back and forth - I guess, and in that context, the name makes sense.是的,
memmove
是副本的另一种变体。它不会“移动”内存,即原始字节不再是操作之前的字节。它是专门处理内存重叠的情况的。如何完成此操作取决于您的体系结构,但我看到它以两种不同的方式完成:
Yes,
memmove
is another variation on the copy. It does not "move" memory in the sense that the original bytes are no longer what they were before the operation. It is specifically to handle the case of overlapped memory.How this is done depends on your architecture but I've seen it done two different ways:
回答关于 memmove() 名称的问题:我认为该名称旨在反映对将数组中的元素范围移动到数组中的另一个范围的支持(例如,为开头或数组内部的新元素)。
正如其他答案提到的,
memcpy()
可以使用memmove()
无法使用的优化技术,因为memcpy()
不支持内存区域重叠,而memmove()
则重叠。您可能需要考虑的一件事是默认情况下仍使用
memmove()
而不是memcpy()
,除非您正在开发复制性能至关重要的应用程序。memcpy()
可以提供更好的性能(我认为主要是因为它可以更有效地内联),但请记住 这段关于 Linux 内核中memcpy()
实现的引用来自关于应用程序错误使用 C 库的讨论memcpy():
To answer your question about
memmove()
's name: I think the name was intended to reflect support for moving ranges of elements in an array to another range in the array (for example, to make room for a new element at the start or inside the array).As other answers mention,
memcpy()
can use optimizations techniques that aren't available tomemmove()
becausememcpy()
doesn't support memory regions that overlap, whilememmove()
does.One thing you might want to consider is to still use
memmove()
instead ofmemcpy()
by default unless you're working on an application where copy performance is critical.memcpy()
can provide better performance (mainly because it can inlined more effectively, I think), but keep in mind this quote about thememcpy()
implementation in the Linux kernel from a discussion about an application's incorrect use of the C library'smemcpy()
: