This article discusses the code implementation of strcpy and memcpy, two common c and C ++ interview questions.

1. The handwritten strcpy

To start, a standard strCPy implementation looks like this:

char *strcpy(char* strDest, const char* strSrc)
{ assert( (strDest ! =NULL) && (strSrc ! =NULL));
	char *address = strDest;
	while((*strDest++ = *strSrc++) ! ='\ 0');
	return address;
}
Copy the code

Here are a few things to note:

  • The source string should be of const type to avoid being modified in the program.
  • An assertion is added to the function entry to check whether the source and destination string Pointers are null, otherwise unexpected errors will occur.
  • Using a while loop should be simple and clear, with as little code as possible;
  • The return value ischar*, and return the original value of the target string pointer, so that the function can support the chain expression, increase the added value of the function.

These points don’t just apply to StrCPy, but we try to follow these rules whenever we write code so that we can write highly usable and robust code.

Strncpy: strncpy: strncpy: strncpy: strncpy: strncpy: strncpy: strncpy: strncpy

char *strncpy(char* strDest, const char* strSrc, size_t n)
{ assert( (strDest ! =NULL) && (strSrc ! =NULL));
	char *address = strDest;
	while( n-- && (*strDest++ = *strSrc++) ! ='\ 0');
	return address;
}
Copy the code

There is a control based on the length of the input, and usually we use the length of the input as the length of the target string minus 1, because there is a space for the terminator ‘\0’.

2. Realization of memCPy

Memcpy implementation can actually refer to the implementation of strncpy, such as we convert pointer type to char* to achieve copy, this way is in accordance with a byte by byte to copy, first or see the code as fast, as follows:

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

struct people
{
	int iAge;
	char szName[12];
	char szSex[3];
};

// Simulate memcpy implementation
void * mymemcpy(void *dest, const void *src, size_t n)
{
    if (dest == NULL || src == NULL)
          return NULL;
    char *pDest = static_cast <char*>(dest);
    const char *pSrc  = static_cast <const char*>(src);
	if (pDest > pSrc && pDest < pSrc+n)
	{
		for (size_t i=n- 1; i ! =- 1; --i) { pDest[i] = pSrc[i]; }}else
	{
		for (size_t i= 0; i < n; i++) { pDest[i] = pSrc[i]; }}return dest;
}

int main(a)
{
	people stPeople1, stPeople2;
	memset((void*)&stPeople1, 0x00.sizeof(stPeople1));
	memset((void*)&stPeople2, 0x00.sizeof(stPeople2));
	stPeople1.iAge = 32;
	mymemcpy(stPeople1.szName, "li lei".sizeof(stPeople1.szName));
	mymemcpy(stPeople1.szSex, "man".sizeof(stPeople1.szSex));

	mymemcpy((void*)&stPeople2, (void*)&stPeople1, sizeof(stPeople2));
	
	printf("this people age is %d, name is %s, sex is %s\n", stPeople2.iAge, stPeople2.szName, stPeople2.szSex);
	
	return 0;
}
Copy the code

Mymemcpy implementation is a byte implementation, but it is not the same as strncpy implementation.

  • Similarly, check at the function entry to see if the source string pointer and target string pointer are null, otherwise unexpected errors will occur;
  • Since it is copied by one byte, the parameter is converted tochar*Type to operate;
  • To see if there are any memory source memory and the target memory overlap, if the target first address in the middle of the source memory, the memory will be from the back forward copy, because if the copy back once upon a time, the first address of the place from the target memory will be overwritten, if there is no overlap, or source memory address is in the center of the target memory, that has nothing to do, can be used to copy back;
  • You can’t use'\ 0'To determine the end of the copy, because it is a copy of a whole block of memory, to take a simple example, if you copy a structure, like the above code, then it is likely to be copied somewhere in the middle and stop, the copy is equivalent to incomplete;
  • Likewise, memcpy returns the destination string address;

But what if the interviewer asks you to copy it in 4 bytes?

According to the logic in front of, in fact, according to the four bytes copy is to convert a pointer type to int * to copy, but there is a little different, if in accordance with the four bytes to copy, can’t do the to judge the memory of overlapping, because the basic unit of memory is a byte, 4 bytes is no way to avoid the coverage, An available 4-byte copy code is as follows:

void * mymemcpy(void *dest, const void *src, size_t n)
{
    if (dest == NULL || src == NULL)
          return NULL;
    int *pDest = (int*)dest;
    int *pSrc  = (int*)src;
	int iWordNum = n/4;
	int iSlice = n%4;
	while(iWordNum--)
	{
		*pDest++ = *pSrc++;
	}
	char* pcDest = (char*)pDest;
	char* pcSrc = (char*)pSrc;

	while(iSlice--)
	{
		*pcDest++ = *pcSrc++;
	}

	return dest;
}
Copy the code

If the number of bytes is 4, copy it as an int. If the number of bytes is not 4, copy it as a char.

Well, the implementation of strcpy and memcpy is introduced here, if my creation is useful to you, please give a thumbs up.