C language Simulation implements Memcpy,memmove function

Source: Internet
Author: User

Here memcpy and memmove function of the simulation implementation, need to use a null pointer to pass the parameter, after the forced type conversion to char type, with SIZE_T this macro accept offset offset, the simulation is implemented as follows:

memcpy function:

void* my_memcpy (Void*dst,const void*src, size_t count) {assert (DST); assert (SRC); void* ret = Dst;while (count--) {* (char* DST = * (char*) SRC;DST = (char*) DST + 1;src = (char*) src + 1;} return ret;}

Memmove function:

The MEMMVE function is to avoid the memory overlap of the memcpy function (mentioned above), and the development of a new function, the idea is that if there is memory overlap, then we copy from the back, if not, as memcpy, the former back copy, the implementation code is as follows:

void* My_memmove (VOID*DST, const VOID*SRC, size_t count) {assert (DST); assert (src); void*ret = Dst;if ((char*) DST > (( char*) src + count)//determine if memory overlap {while (count--) {* ((char*) DST + count) = * ((char*) src + count);}} Else{while (count--) {* (char*) DST = * (char*) SRC;DST = (char*) DST + 1;src = (char*) src + 1;}} return ret;}

A copy of the memory, more than a copy of the string copy offset parameter, because the string copy is often encountered 0 stop copy, and memory copy does not exist such problems, the use of these two kinds of functions need to think how to effectively let the program run.

If there is any shortage, I hope to criticize.

This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1714495

C language Simulation implements Memcpy,memmove function

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.