Are there any problems with this memcpy() implementation? I received this question at a C++ programming interview:

void memcpy(char* dst, const char* src, int numBytes)
{ 
    int* wordDist = (int*)dst;
    int* wordSrc = (int*)src;
    int numWords = numBytes &gt;&gt; 2;  
    for (int i = 0; i &lt; numWords; i++)
    {
        *wordDist++ = *wordSrc++;  
    }
    int whatisleft = numBytes &gt;&gt; 2 - (numWords );
    dst = (char*)wordDist;
    src = (char*)wordSrc;
    for (int i = 0 ; i &lt;= whatisleft; i++);
    {
        *dst++ = *src++;
    }
}



What are portability issues? 
At least 1 syntax error?
Algorithm considerations?


Now I found that this line:

int whatisleft = numBytes &gt;&gt; 2 - (numWords );


is wrong as it doesn't calculates the correct number of bytes left to copy.

The correct one should be:

int numBytesInWord = (numWords &lt;&lt; 2 );
int whatisleft = numBytes  - numBytesInWord;


At the end, the copied char array is several characters shorter than the source.
There's a number of issues with this code:

int* wordDist = (int*)dst;
int* wordSrc = (int*)src;


Even if this works on a given platform, it is definitely not portable. int* can often have more strict alignment requirements than char* - this is technically undefined behaviour.

Further, (int*)src; is casting away constness - always dangerous if you then modify the value somewhere in the rest of the function.

int numWords = numBytes &gt;&gt; 2; 


This assumes that an int is 32 bit, which is again non-portable, and will of course be wrong on any platform where this isn't the case. This could be fixed by using:

int numWords = numBytes / sizeof(int);


However, the alignment issues mentioned previously still remain.

This code:

int whatisleft = numBytes &gt;&gt; 2 - (numWords);


is completely wrong, as thanks to operator precedence, it is equivalent to numBytes &gt;&gt; (2 - numWords) which is likely to be negative, causing undefined behaviour. Also, one has to be careful regardless, and keep in mind the differences between an arithmetic right shift and a logical right shift. Again, technically, right shifts for signed values are implementation defined, and could potentially be either.