memcpy function Explanation

Source: Internet
Author: User

Function Prototypes:

void *memcpy (void *dest,void *src, unsigned int count)

{

ASSERT ((dest!=null) && (src!=null));

if (DEST==SRC)

return SRC;

char* d= (char*) dest;

char* s= (char*) src;

while (count--> 0)

*d++=*s++;

return dest;

}

This is a memcpy source code that generates a temporary pointer inside a function that does not change the original pointer

Parameter description: Dest is the destination string, SRC is the source string, and count is the number of bytes to copy.

function function: Copies the first n bytes of the string src into the dest.

Return Description: SRC and dest memory area can overlap, function returns void* type pointer.

function is the same as memcpy. The difference is that when the memory areas referred to by SRC and dest overlap, the memmove () can still be handled correctly, but the execution efficiency is slightly slower than using memcpy ().

memcpy (), Memmove () and memccpy ()

-------------------------------------------------------

The function of these three functions is to copy a block of memory to another memory block. The difference between the first two functions is that they handle memory area overlap (overlapping) in different ways. The function of the third function is also to replicate memory, but stop copying immediately if a particular value is encountered.

For a library function, you should use the Memmove () function because there is no way to know what is being passed to his memory area. With this function, you can guarantee that there will be no memory block overlap problem. For applications, it is safe to use the memcpy () function because the code "knows" that two blocks of memory do not overlap.

-------------------------------------------------------

#include <string.h>

#include <stdio.h>

int main ()

{

Char s[] = "Zengxiaolong";

Memmove (S, S+4, strlen (s)-4);

S[strlen (s)-4] = ' + ';

printf ("*s =%s\n", s);

return 0;

}

The difference between memcpy and strncpy

strncpy is to copy num characters from SRC to dest, but if it encounters the SRC character end, then the copy is terminated prematurely, the characters that are not copied are not processed, and of course the DEST,SRC address cannot overlap.

memcpy also copies num characters from SRC to dest, but it is a memory copy, whether or not NULL

RELATED Links: http://blog.csdn.net/cq20110310/article/details/7032104

memcpy function Explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.