Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If destination and source are the same, what does memmove do?

Tags:

c

memmove

realloc

If destination and source are the same, does memmove still "move" the data (or does it return directly)? What about realloc; what if the new size is the same as the old size?

like image 417
Paul Manta Avatar asked Dec 01 '11 13:12

Paul Manta


People also ask

What does Memmove return in C?

Return Value The memmove() function returns a pointer to dest .

What is the use of Memmove?

Description. The memmove() function copies count bytes of src to dest. This function allows copying between objects that might overlap as if src is first copied into a temporary array.

How memcpy () and Memmove () do compare in terms of performance?

That memmove might be slower than memcpy is because it is able to handle overlapping memory, but memmove still only copies the data once. profile it on the platform you're interested in the timings for. However, the chances of you writing a better memmove than memmove seems unlikely.

What is the difference between the Memmove () and memcpy () function?

Answer: memcpy() function is is used to copy a specified number of bytes from one memory to another. memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.


2 Answers

That's really going to be implementation-specific. It would be good practice to do so, sure, but it really depends which implementation you mean.

It's going to work either way, but presumably a suitably clever implementation would check for overlapping segments (and particularly for the case where source == dest) and deal with it appropriately.

like image 77
Gian Avatar answered Sep 29 '22 06:09

Gian


As far as I know no standard gives any promises about returning immediately in such case, so you should not expect this behavior.

Better do not pass invalid pointers in hope it's not going to access the data ;-)

like image 24
Michael Krelin - hacker Avatar answered Sep 29 '22 06:09

Michael Krelin - hacker