Qualcomm Interview Question
Software Engineer / Developerswhat about memory alignment check while accessing lets say 4 bytes for int? the src and dest pointer should be only copied as int if they are aligned on the same boundary. For your solution assumption is that processor allows unaligned memory accesses which will make you better performance version bad
Basic first version - copies one byte at a time.
- Girish September 09, 2008void memcpy(void* src, void* dst, size_t len)
{
char* p = (char*)src;
char* q = (char*)dst;
while(len--)
*p++ = *q++;
}
Version 2 - copies int at a time - better performance.
void memcpy(void* src, void* dst, size_t len)
{
/* Copy whatever can be copied as integers */
size_t nTimes = len/sizeof(int);
int* pIntSrc = (int*) src;
int* pIntDst = (int*) dst;
while( nTimes-- )
*pIntSrc++ = *pIntDst++;
/* copy the remainder */
nTimes = len % sizeof(int);
char* pCharSrc = (char*) pIntSrc;
char* pCharDst = (char*) pIntDst;
while( nTimes-- )
*pCharSrc++ = *pCharDst++;
/* Done */
}