Memset memcpy strcpy

Source: Internet
Author: User

 

 

# Include "memory. H"

Memset is used to set all memory spaces to a specific character. It is generally used to initialize the defined string to ''or '/0'. For example, char a [100]. memset (A, '/0', sizeof ());

Memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, a, sizeof (B); note that if sizeof (a) is used, the memory address of B may overflow.

Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); if strcpy (B, A) is used, check whether the length of the string in A (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.

Strcpy
Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # include <string. h>
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # include <string. h>
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
Memset
Prototype: extern void * memset (void * buffer, int C, int count );
Usage: # include <string. h>
Function: sets the first count byte of the memory area referred to by buffer to character C.
Note: The pointer to the buffer is returned.

 

1. Void * memset (void * s, int C, size_t N)
Purpose: set the value of the first n Bytes of the memory space S to the value c.

2. Example

Main (){
Char * s = "golden Global View ";

Clrscr ();

Memset (S, 'G', 6); // it seems that this is a problem //
Printf ("% s", S );

Getchar ();
Return 0;
}
3. The memset () function is often used for memory space initialization. For example:
Char STR [100];
Memset (STR, 0,100 );

4. Deep connotation of memset (): it is used to set all memory space to a specific character. It is generally used to initialize the defined string to 'memset (A, '/0 ', sizeof ());

Memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, a, sizeof (B); note that if sizeof (a) is used, the memory address of B may overflow.

Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); if strcpy (B, A) is used, check whether the length of the string in A (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.

5. Supplement: some of your experiences
Memset can easily clear a variable or array of the structure type.

For example:
Struct sample_struct
{
Char csname [16];
Int iseq;
Int itype;
};

For Variables
Struct sample_strcut sttest;

In general, the method of clearing sttest is as follows:
Sttest. csname [0] = '/0 ';
Sttest. iseq = 0;
Sttest. itype = 0;

Memset is very convenient:
Memset (& sttest, 0, sizeof (struct sample_struct ));

If it is an array:
Struct sample_struct test [10];
Then
Memset (test, 0, sizeof (struct sample_struct) * 10 );

6. Strcpy
Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # I nclude
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # I nclude
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
Memset
Prototype: extern void * memset (void * buffer, int C, int count );
Usage: # I nclude
Function: sets the first count byte of the memory area referred to by buffer to character C.
Note: The pointer to the buffer is returned.

 

 

 

 

 

Usage of the memset function:

 

Set the value of the first n Bytes of memory space s to C

The following is an example.

# Include <stdio. h>
# Include <string. h>
Main (){
Char * s = "golden Global View ";

Clrscr ();

Memset (S, 'G', 6 );
Printf ("% s", S );

Getchar ();
Return 0;
}

The memset () function is often used for memory space initialization. For example:

Char STR [100];
Memset (STR, 0,100 );

 

Memset is used to set all memory spaces to a specific character. It is generally used to initialize the defined string to ''or '/0'. For example, char a [100]. memset (A, '/0', sizeof ());
Memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, a, sizeof (B); note that if sizeof (a) is used, the memory address of B may overflow.

Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (A, B ); if strcpy (B, A) is used, check whether the length of the string in A (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.

# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main ()
{
Char A [5];
Memset (A, '1', 5 );
For (INT I = 0; I <5; I ++)
Cout <A [I] <"";
System ("pause ");
Return 0;
}
However, it is not feasible to set the element value in the array to 1 in the following program.
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main ()
{
Int A [5];
Memset (A,); // it cannot be changed to memset (A, * sizeof (INT) here.
For (INT I = 0; I <5; I ++)
Cout <A [I] <"";
System ("pause ");
Return 0;
}

Because the array a of the first program is of the bytes type, the memory occupied by the memory size is 1 byte, And the memset function is also assigned a value in bytes, so there is no problem with your output.
The second program A is an integer, and memset or byte value is used. After the value is assigned, the value of each array element is actually 0x01010101, that is, decimal 16843009.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/crfoxzl/archive/2008/09/09/2906043.aspx

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.