Memcpy () function in C Language

Source: Internet
Author: User

Yesterday I was asked a very basic C language question.
Write the definition of u8 * memcpy (u8 * SRC, u8 * DST, size_t Len.

I thought I had to consider a lot of things, so I thought about a lot of things.
Generally, it is written as void *, which is forcibly converted to an 8bit type. In fact, the unsigned char type is used here, and the forced conversion is no longer needed.

Whether SRC and DST are empty. I will use if to judge. If SRC is empty, an error occurs. If DST is empty, how can this problem be solved.
If we apply for a space for DST, who will release it?
Because it is prone to memory leakage and the requested space of the called function is not available, an error is reported directly.. The caller is responsible for applying for a space and revoking it to ensure Memory Security.

You can set assert.:
Assert (DST! = NULL | SRC! = NULL );

If you use
* SRC ++ = * DST ++;
You need to define another pointer pointing to the first byte of DST for the return pointer.
You can also define temporary variables and use the for loop.
* (SRC + I) = * (DST + I );
In this case, DST is not changed, and the result is returned directly.

What if SRC and DST are overwritten under LEN Length?
* SRC |
* DST |

In fact, in the library function, memcpy cannot change the source data, and an error should be reported at this time.
Only in memmove can SRC be overwritten.
But according to his instructions, I covered the repeated areas.
In fact, if you use a for loop, you can directly copy from the last forward without overwriting. The result is always correct.Since the copy operation starts from the highest bit, this ensures that the blocks not copied in SRC are not modified due to DST.

If you call the database functions memcpy () and memmove (), note the following:

The memcpy () function copies Len characters from the area pointed to by Src to the area pointed to by DST. If the two arrays overlap, this function is not defined.

In memmove (), if the two functions overlap, the value assignment is still correct.

the memcpy function assumes that the memory area to be copied does not overlap, if you can ensure that the memory area of your replication operation does not overlap, you can directly use emcpy;
if you cannot ensure that there is overlap, and the source array is no longer used , to ensure the correctness of the replication, you must use memmove.

Related Article

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.