Memory operating tools: memset, memmove, and memcpy

Source: Internet
Author: User

I. function declaration: void * memset (void * s, int C, size_t N );

Memset: sets all the content of each byte in the memory to the ASCII value specified by CH,
The block size is specified by the third parameter. This function initializes the newly applied memory and returns a pointer to S.

1.1: general understanding

The most commonly used method is to initialize an array.

For example

Int data [100];

Memset (data, 0, sizeof (data ));

The purpose of the function is to initialize all the elements of the data array to 0. Some people often write the third parameter: sizeof (INT) * 100; in fact, the same is true. The reason is described later.

1.2: points worth rectification and attention

1. Example: memset (data, 1, sizeof (data )). Apparently, the purpose is to initialize all the elements of data to 1. After experiment, we found that the memory contains a large number of 16843009 errors.

Not the one we want.

Cause analysis: The memset function assigns values in bytes. It is often used to assign values to all elements of a string.

For example

Char data [20];

Memset (data, '#', sizeof (data ));

The data in Example 1 is an integer type, and memset or byte assignment is used to assign values to the 100 bytes of memory pointed to by data. Each value is filled with the ASCII character 1, after being converted to binary, 1 is 00000001, which occupies one byte. An int element is 4 bytes, starting with 00000001000000010000000100000001, which is equal to 16843009. Then, the value of an int element is assigned. After the value is assigned, the value of each array element is actually 0x01010101, that is, 16843009 in decimal format. Check whether the output result is like this? Therefore, you must remember that memset is replicated in bytes. Therefore, the two statements about the third parameter mentioned above are correct, and they should be the number of bytes occupied by this array. Therefore, you can directly write the third parameter 100 (for the previous example ).
We can also feel that memset is used to initialize an integer array to 0, which is entirely made by people who know more about memory or are opportunistic. We must be careful when using it and understand its principles. Otherwise, many mistakes will be made.

To sum up, memset actually operates on a single byte. Similar to strset. Only strset can operate on strings. Memset can be of any type. It cannot be used in disorder.


Ii. Void * memcpy (void * DEST, const void * SRC, int N );

Memcpy: Copy n Bytes from the starting position of the memory address indicated by the source SRC to the starting position of the memory address indicated by the target DeST. Note:

1. The source and DESTIN memory regions cannot overlap. The function returns a pointer to Destin.

2. The differences between strcpy and memcpy are as follows.

2.1 The copied content is different. Strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, struct, and classes.

2.2 The replication method is different. Strcpy does not need to specify the length. It ends with the string Terminator "\ 0" of the copied character, so it is prone to overflow. Memcpy decides the copy Length Based on its 3rd parameters.

2.3 different purposes. Strcpy is usually used to copy strings, while memcpy is generally used to copy data of other types.

3. If the target array DESTIN already has data, after memcpy () is executed, the original data will be overwritten (N at most ). If you want to append data, you need to add the target array address to the address where you want to append the data after executing memcpy.

Note that both source and DESTIN are not necessarily arrays, and any space that can be read and written can be used. This is an amazing place.


3: void * memmove (void * DEST, const void * SRC, size_tcount
);

Memmove: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST. Memmove supports overlapping memory on both ends. Therefore, memmove is safer than memcpy.

Source code with memcpy and memmove:

void * __cdecl memcpy (        void * dst,        const void * src,        size_t count        ){        void * ret = dst;         /*         * copy from lower addresses to higher addresses         */        while (count--) {                *(char *)dst = *(char *)src;                dst = (char *)dst + 1;                src = (char *)src + 1;        }         return(ret);} void * __cdecl memmove (        void * dst,        const void * src,        size_t count        ){        void * ret = dst;         if (dst <= src || (char *)dst >= ((char *)src + count)) {                /*                 * Non-Overlapping Buffers                 * copy from lower addresses to higher addresses                 */                while (count--) {                        *(char *)dst = *(char *)src;                        dst = (char *)dst + 1;                        src = (char *)src + 1;                }        }        else {                /*                 * Overlapping Buffers                 * copy from higher addresses to lower addresses                 */                dst = (char *)dst + count - 1;                src = (char *)src + count - 1;                 while (count--) {                        *(char *)dst = *(char *)src;                        dst = (char *)dst - 1;                        src = (char *)src - 1;                }        }         return(ret);}


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.