Memest Prototypes (Please type "mans memset" in your shell)
void *memset (void *s, int c, size_t n);
Memset: The effect is to populate a block of memory with a given value, which is the fastest way to clear 0 operations on larger structures or arrays.
Three common types of errors
First: The position of C and N is reversed.
Be sure to remember that if you want to clear a char a[20], it must be memset (A, 0, 20)
Instead of memset (a, 20, 0)
Second: Excessive use of memset, I think these programmers may have some sort of psychological shadow, they fear uninitialized memory, so they will write this code:
Char buffer[20];
memset (buffer, 0, sizeof (char) *20);
strcpy (buffer, "123");
The memset here are superfluous. Because this memory is immediately covered, clear 0 is meaningless.
Third: In fact, this error strictly speaking can not be used wrong memset, but it often in the use of memset occasions appear
int Some_func (struct something *a) {
...
...
memset (A, 0, sizeof (a));
...
}
q : Why use memset to zero? memset (&address, 0, sizeof); often see this usage, in fact, when allocating data, the rest of the space will be zeroed.
Answer: 1. If not emptied, a wild value may be present in the test. You do the following test to see the results ()
Char buf[5];
CString str,str1;//memset (buf,0,sizeof (BUF));
for (int i = 0;i<5;i++)
{str. Format ("%d", Buf[i]); str1 +=str;}
TRACE ("%s\r\n", str1)
2. It's not! Especially for the character pointer type, the remainder is usually not 0, it is advisable to make an experiment, define a character array, and enter a string of characters, if not memset implementation of zero, using the MessageBox display will be garbled (0 means null, if there is, the default character ends, Does not output the subsequent garbled)
Ask:
The following demo is possible to set the element values in the array to character 1,
1#include <iostream>2#include <cstring>3 using namespacestd;4 intMain ()5 {6 Chara[5];7Memset (A,'1',5);8 for(inti =0; I <5; i++)9cout<<a[i]<<" ";TenSystem"Pause"); One return 0; A}
And, the following program to think about the element value in the array is set to 1, but it is not feasible
1#include <iostream>2#include <cstring>3 using namespacestd;4 intMain ()5 {6 inta[5];7Memset (A,1,5);//It is not possible to change this to memset (a,1,5 *sizeof (int)).8 for(inti =0; I <5; i++)9cout<<a[i]<<" ";TenSystem"Pause"); One return 0; A}
The problem is:
1, why the first program is possible, and the second one is not,
2, do not want to use for, or while loop to initialize int a[5]; Can you do that? (There is a function initialization like memset ())
For:
1. Because the first program's array A is a character type, the character type occupies memory size is 1Byte, and the Memset function is also assigned in bytes, so you output no problem. and the second program A is integer, using memset or byte assignment, so that after the assignment, the value of each array element is actually 0x01010101, or decimal 16843009. Would you like to see your output?
2. If using memset (a,1,20);
is to assign a value of 20 bytes to a point of memory, each with ASCII 1 characters to fill, to binary, 1 is 00000001, accounting for one byte. An int element is 4 bytes, the unity is 1000000010000000100000001, equal to 16843009, the assignment of an int element is completed.
Memest function Usage description (go from Lee.kevin)