Memset
Directory [hide]
-
Merit
-
Usage
-
Program example
-
Memset Functions
[Edit this section]
Merit
Set 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,
The return value is the pointer to S.
[Edit this section]
Usage
Void * memset (void * s, int CH, unsigned N );
[Edit this section]
Program example
# Include <string. h>
# Include <stdio. h>
# Include <memory. h>
M E M S E T Letter Quantity
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 [I] <"";
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 memset operates 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 [I] <"";
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 00000001000000010000000100000001, 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!
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:
Memset (x, 0, sizeof (some) * 10 );
[Edit this section]
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;
}
[It should be okay. The string pointer can be the same. It is not a read-only memory and can run normally]
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. You can 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 );