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

问题描述

我阅读了有关memcpy()和memmove()之间差异的常见问题解答。

显然memmove()应该更安全。只是为了确保我理解memmove()的概念,有人能告诉我这个

小函数是否正确使用了memmove()?


void strdel(char * s,size_t offset,size_t count){

if((!s)||(offset> strlen(s))||(count> ; strlen(s)))返回;

memmove(s + offset,s +(count + 1),strlen(s)-count);

}

该函数将删除给定位置的字符串的一部分。


例如:


char tmpstr [10 ] =" Hello";

strdel(tmpstr,0,0);


这会改变你好到了ello。


它似乎工作正常,但我只是想确保我使用

memmove()在这种情况下是正确的,并且该功能无法打开

某种溢出...


谢谢

解决方案




如果调用不正确(虽然它不是必须保护所有

可以防止被错误调用。


看起来s [offset]是要删除的第一个字节,s [count]是最后一个

要删除的字节?


然后,如果count == offset,则删除一个字节,如果count == offset - 1,则

删除0字节。 />

但是如果计数<偏移 - 1你最后向右移动字节,所以你可以

用完空间。


-

RSH






但是没有memmove()应该保护这种情况不发生?我想

memmove()的全部目的是提供一种安全的方式来复制

内存...


memmove()的手册页说明如下:


" memmove()函数将字符串src中的len个字节复制到字符串dst。

两个字符串可能重叠;副本总是以非破坏性的方式完成。

我认为这意味着无论给出什么偏移和计数,memmove()

负责确保没有出错;无论如何。





所有库函数做他们应该做的事情,

,当你用正确的论据打电话时。


如果你在一个arrray中有一个字符串:

char array [] =" abc";

你要将第三个字符复制到第一个字符,

以便它变成cbc ;,然后你可以使用memcpy:


memcpy(数组,数组+ 2,1);

如果你在arrray中有一个字符串:

char array [] =" abc";

你要将最后两个字符复制到

前面,以便它变为 bcc",然后使用memmove:


memmove(数组,数组+ 1,2);


....因为m当源和目的地重叠时,emcpy未定义




-

pete


I read the FAQ about the differences between memcpy() and memmove().
Apparently memmove() is supposed to be safer. Just to make sure I
understand the concept of memmove(), can someone tell me if this
little function is using memmove() correctly?

void strdel(char *s, size_t offset, size_t count) {
if ((!s) || (offset > strlen(s)) || (count > strlen(s))) return;
memmove(s+offset,s+(count+1),strlen(s)-count);
}
The function will delete a portion of a string at the give positions.

For example:

char tmpstr[10] = "Hello";
strdel(tmpstr,0,0);

This would change "Hello" to "ello".

It appears to work correctly, but I just want to make sure I am using
memmove() correct in this case, and that the function is not open to
an overflow of some kind...

Thanks

解决方案



It is if called incorrectly (though it''s not obligatory to protect all
functions against being called wrongly).

Looks like s[offset] is the first byte to delete, and s[count] is the last
byte to delete?

Then if count == offset you delete one byte, and if count == offset - 1 you
delete 0 bytes.

But if count < offset - 1 you end up moving bytes to the right, so you can
run out of space.

--
RSH





But isn''t memmove() supposed to protect that from happening? I thought
the whole purpose of memmove() was to provide a safe way to copy
memory...

The man page for memmove() says the following:

"The memmove() function copies len bytes from string src to string dst.
The two strings may overlap; the copy is always done in a non-
destructive manner."
I take that to mean no matter what offset and count is given, memmove()
is responsible for making sure that nothing goes wrong; no matter what.




All library functions do what they''re supposed to do,
when you call them with correct arguments.

If you have a string in an arrray:
char array[] = "abc";
and you want to copy the third character onto the first,
so that it becomes "cbc", then you can use memcpy:

memcpy(array, array + 2, 1);
If you have a string in an arrray:
char array[] = "abc";
and you want to copy the last two characters to the
front so that it becomes "bcc", then use memmove:

memmove(array, array + 1, 2);

.... because memcpy is undefined
when the source and destination overlap.

--
pete


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

10-15 03:38