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.
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.
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);
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.
"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.
Since the running time of memmove
increases in direct proportionality with the number of bytes it is required to move, it is O(n).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With