cmemmove

overlapping memmove best practice


I have a question about the 3rd parm to memmove when manipulating a string. strlen(string) and strlen(string) + 1 seem to produce the same results. I would think that the +1 part would include a termination \0 but everything seems to work fine with both. Is there a best practice here to consider?? I've seen examples of both and I am not sure which way to go on this??

Consider the following c program:

#include    <stdio.h>
#include    <string.h>
#include    <stdlib.h>
#include    <errno.h>

int main()
{
   char string1[20]="Hello World";
   char string2[20]="Hello World";
   printf("\nstring1=\"%s\"\n",string1);

   memmove(string1,string1+6,strlen(string1) + 1);

   printf("\nstring1 after memmove \"%s\" using strlen(string1) + 1\n",string1);

   printf("\nstring2=\"%s\"\n",string2);

   memmove(string2,string2+6,strlen(string2));

   printf("\nstring2 after memmove \"%s\" using strlen(sting2)\n",string2);

   return 0;
}

Output:

string1="Hello World"

string1 after memmove "World" using strlen(string1) + 1

string2="Hello World"

string2 after memmove "World" using strlen(sting2)

Solution

  • Since you're starting at index 6 both strlen(...) and strlen(...) + 1 are overkill and are copying extra NUL bytes past the end of the strings. It happens to work because you made oversized char[20] arrays so there are indeed extra NULs. Extra array slots are filled with zeros as if you'd written:

    char string1[20] = "Hello World\0\0\0\0\0\0\0\0\0";
    

    You should be subtracting 6 from both. If you do that you'll see that strlen(... + 6) + 1 or strlen(...) + 1 - 6 are what you want. Remove the + 1 from either of those and it won't copy the NUL terminator, leading to a different result:

    string1 == "World\0World\0\0\0\0\0\0\0\0\0"  // with + 1, NUL copied
    string2 == "World World\0\0\0\0\0\0\0\0\0"   // without + 1, NUL missing