Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I consider memmove() O(n) or O(1)?

this may be a silly question, but I want to calculate the complexity of one of my algorithms, and I am not sure what complexity to consider for the memmove() function.

Can you please help / explain ?

void * memmove ( void * destination, const void * source, size_t num );

So is the complexity O(num) or O(1). I suppose it's O(num), but I am not sure as I lack for now the understanding of what's going on under the hood.

like image 627
Andrei Ciobanu Avatar asked Apr 25 '10 21:04

Andrei Ciobanu


People also ask

What does Memmove function do?

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. The memmove() function returns a pointer to dest . This example copies the word "shiny" from position target + 2 to position target + 8.

What is memmove in c++?

memmove() is used to copy a block of memory from a location to another. It is declared in string.h. // Copies "numBytes" bytes from address "from" to address "to" void * memmove(void *to, const void *from, size_t numBytes);

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.

Is memcpy faster than Memmove?

"memcpy is more efficient than memmove." In your case, you most probably are not doing the exact same thing while you run the two functions. In general, USE memmove only if you have to. USE it when there is a very reasonable chance that the source and destination regions are over-lapping.


2 Answers

Since the running time of memmove increases in direct proportionality with the number of bytes it is required to move, it is O(n).

like image 136
Lasse V. Karlsen Avatar answered Nov 08 '22 18:11

Lasse V. Karlsen


What are you applying the memmove() operation to - selected elements in the algorithm or all of them? Are you applying memmove() to elements more than once?

Those are the things that will matter to the algorithm's complexity.

That answer may be different that the complexity of memmove() itself regarding the arrays of char elements that memmove() deals with (for which memmove() is an O(n) operation).

like image 20
Michael Burr Avatar answered Nov 08 '22 19:11

Michael Burr