Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the "const void*" mean in memmove?

The second arg in the prototypes for memmove/memcpy/strcpy are similar: For example:

void *memmove(void *dest, const void *src, size_t n); //const void*
char *strcpy(char *dest, const char *src); //const char*

But apparently, if dest and src overlap, then src's content will be altered, violating the const void/char *?

like image 643
Alcott Avatar asked Sep 16 '11 13:09

Alcott


3 Answers

const void* means that the referand will not be modified through that pointer.

If there are other, non-const pointers to the same object (also known as "aliasing"), then of course it can still be modified through those. In the scenario you describe, that other pointer is dest.

By the way, in the case of strcpy, behavior is undefined if the regions overlap, and in C99 the signature is char *strcpy(char * restrict s1, const char * restrict s2);. But for memmove, aliasing is OK. By giving it overlapping regions you've given it "permission" to modify the dest region, and it will do that.

like image 134
Steve Jessop Avatar answered Sep 28 '22 08:09

Steve Jessop


The argument is marked const void * to indicate memmove will never modify the memory pointed to by src using that pointer. If overlap occurs the memory is modified using the dest pointer, not the src pointer, so the guarantee is not violated.

like image 24
Praetorian Avatar answered Sep 28 '22 08:09

Praetorian


It means memmove guarantees it won't directly modify the memory pointed by src.

Of course if the two blocks overlap memmove will change the so-called "const" memory. const is ca contract attached to a name. There's no way to make the actual memory read-only.

like image 30
cnicutar Avatar answered Sep 28 '22 06:09

cnicutar