Memset ()

Source: Internet
Author: User

Function: set 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 usually initializes the newly applied memory.

Usage: void * memset (void * s, char CH, unsigned N );

Program example:

# Include <string. h>

# Include <stdio. h>

# Include <mem. h>

Int main (void)

{

Char buffer [] = "Hello world \ n ";

Printf ("buffer before memset: % s \ n", buffer );

Memset (buffer, '*', strlen (buffer ));

Printf ("buffer after memset: % s \ n", buffer );

Return 0;

}

Output result:

Buffer before memset: Hello World

Buffer after memset :***********

Compilation platform:

Microsoft Visual C ++ 6.0

It is not necessarily to set all the content to the ASCII value specified by CH, And the CH here can be Int or other types, not necessarily char type. For example:

Int array [5] = {1, 4, 3, 5, 2 };

For (INT I = 0; I <5; I ++)

Cout <Array<"";

Cout <Endl;

Memset (array, 0, 5 * sizeof (INT ));

For (int K = 0; k <5; k ++)

Cout <Array[K]<"";

Cout <Endl;

The output result is: 1 4 3 5 2

0 0 0 0 0

The following table size parameters are in bytes. Therefore, for Int or other parameters, the default value is not 1 (bytes. In addition, the int size on different machines may be different, so it is best to use sizeof ().

Note that the memset operation is performed on bytes, so if the above program is changed

Int array [5] = {1, 4, 3, 5, 2 };

For (INT I = 0; I <5; I ++)

Cout <array <"";

Cout <Endl;

Memset (array, * sizeof (INT); // note that this is different from the above program

For (int K = 0; k <5; k ++)

Cout <Array[K]<"";

Cout <Endl;

The output result is: 1 4 3 5 2

16843009 16843009 16843009 16843009 16843009

Why?

Because memset is to assign values to the five bytes of memory pointed to by array in bytes, each of which is filled with characters with ASCII 1. After being converted to binary, 1 is 00000001, occupies one byte. An int element is 4 bytes, starting with 1000000010000000100000001, which is equal to 16843009. Then, the value of an int element is assigned.

Therefore, using memset to assign initial values to non-struct arrays is not advisable!

The upstairs is right, but the execution result of the program is 0 0 0 0; the difference is not there. The procedure is as follows:

Int array [5] = {1, 4, 3, 5, 2 };

For (INT I = 0; I <5; I ++)

Cout <array <"";

Cout <Endl;

Memset (array, * sizeof (INT); // here is the difference

For (int K = 0; k <5; k ++) // The difference is not here. k = 1 is just a loop.

Cout <array [k] <"";

Cout <Endl;

For example, a struct of some X can be cleared as follows:

Memset (& X, 0, sizeof (some ));

If it is a struct array some X [10], you can do this:

Menset (x, 0, sizeof (some) * 10 );


Memset Functions

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;

}

[This problem is quite big, and the program cannot run at all. Your s ambition here is a read-only memory, and your memset tries to modify it again, so an error occurs during the operation, how to modify char * s to Char s []

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 ); for example, 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.

Link: http://baike.baidu.com/view/982208.htm
<======================================================== ========================================================== ==================================>

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 ); for example, 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.

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.