C language -memset ()

1. memset () function prototype extern void * memset (void * buffer, int c, int count) buffer: or pointer array,

              c: is the value assigned to the buffer,

       count: is the length of the buffer.


       The multi-function socket for emptying such arrays: Prototype is memset (buffer, 0, sizeof (buffer))

       Memset memory section is used to set all of a character, generally used in the definition of strings is initialized to 'or' / 0 ';

      例:char a[100];memset(a, ‘/0’, sizeof(a));

    memset easy emptying of a variable or array type configuration.

Such as:

struct sample_struct
{
char csName[16];
int iSeq;
int iType;
};

 

For variables:
struct sample_strcut stTest;

Generally, empty stTest method:

stTest.csName [0] = '/ 0';
stTest.iSeq = 0;
stTest.iType = 0;

Very easy using memset:
memset (& stTest, 0, the sizeof (struct sample_struct));

 

If the array:

struct sample_struct TEST[10];

memset(TEST,0,sizeof(struct sample_struct)*10);

 

2. Question: "s the contents of each byte pointed to a piece of memory to all of the specified ASCII value ch, the block size specified in the third argument, the function initializes the memory usually do new applications working usage: void * memset (void * s, char ch, unsigned n); "


// "The content of each byte s pointed to a piece of memory all set to ch specified ASCII value of" What? Why say "this function is usually to do the initial work for the new memory applications."?

A: Just the allocated memory space, or are you used memory space inside the data is not fixed in order to avoid these useless data to impact their program, you can use these memory memset inside the data set to a under normal circumstances a value is set to 0, of course, if your program does not affect by these useless data, do not do this job so-called "initialization" of course, refers to the spatial variables or application-defined giving you what you expect value, for example, the statement int i = 0; it indicates defines a variable i, and is initialized to 0; if int j = 5; to show that defines a variable j, and is initialized to 5.

       But for large pieces of allocated memory, this approach of course not, for example, int arr [100]; defines an array ARR, comprising 100 elements, if you wrote int arr [100] = 0; the entire contents of the array like initialized 0, it is not enough, even the compiler can not pass. Initialization in this case, there are two methods, one is a initialization, such as for (int i = 0; i <100; i ++) arr [i] = 0; array initialization is completed. Another method is to use memset: a statement is enough -memset (arr, 0, sizeof ( int) * 100);
the parameters are explained as follows: arr is the first address of the array, 0 is talk about the content of these addresses assignment is 0, sizeof (int) determined type int length, multiplied by 100 to represent the entire length of the array arr.


         Of course, if the memory allocated by malloc, generally only used to memset initialized with a first initialization method clearly inappropriate.

         Example: char ch [10]
         For example memset (ch, 0,8), the former is set to zero eight ch array, the latter is not necessarily zero. Such as beginning ch [1] = 'z' , ch [8] = 'a', ch [9] = 'b', after memset, ch [1] is zero, while ch [8], ch [ 9] will remain unchanged.

 

3.memset function details 
       1) void * memset (void * s, int c, size_t n)
        Total Effects: s memory space has opened the first n-byte value to values, c.

       2) .memset () function is used to initialize the memory space. Such as:
           char STR [100];
           Memset (STR, 0,100);


       3) .memset variable or array can be easily emptied a structure type.

           如:
           struct sample_struct{
                      char csName[16];
                       int iSeq;
                       int iType;
           };

 

           For variables:
           struct sample_strcut stTest;

           In general, the method of emptying stTest:
           stTest.csName [0] = '/ 0';
           stTest.iSeq = 0;
           stTest.iType = 0;

           Very easy using memset:
           memset (& stTest, 0, the sizeof (struct sample_struct));

 

           If the array is:
           struct sample_struct the TEST [10];
           the
           memset (TEST, 0, sizeof ( struct sample_struct) * 10);

           #include <mem.h>
           void* memset(void* s, int c, size_t n){
                     unsigned char* p = (unsigned char*) s;

                     while (n > 0) {
                                *p++ = (unsigned char) c;
                                  –n;
                      }

                     return s;
           }

 

 
        Memset function (), which can be a byte by byte to the entire array of a specified value. Memset () function mem.h declared in the header file, it is the starting address of the array as its first parameter, the second parameter is an array of set value of each byte, the third parameter is the length of the array ( number of bytes, not the number of elements). Which is a function prototype:
    void * Memset (void *, int, unsigned);
  wherein the void * indicates the address.
  For example, the following code is used to make an array of transfer parameters to the standard function Memset (), to let the array is set to full 0:
    #include <mem.h>
    void main ()
    {
     int IA1 [50];
     int IA2 [500];
     Memset (IAI, 0,50 * the sizeof (int));
     Memset (* ia2,0,500 the sizeof (int));
     //
    }
  the first argument Memset () is the array name, i.e., an array of arrays masterpiece parameter as a parameter, it is only just starting address of an array.
  In Memset function () stack region, from the return address order of 1, 2 up parameter. SUMMARY first parameter is the starting address of the array ia1 main () function is defined. The second parameter is a value (0) array provided, the third parameter is the length of the array (50 * 2). When the function returns, the contents of the array main () function is set to all 0.
Stack
0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}”>


memset function details

1. void * memset (void * s, int c, size_t n)
Total Effects: s memory space has opened the first n-byte value to values, c.

2. Examples
#include

void main(){
char *s=”Golden Global View”;

clrscr();

memset(s,’G’,6);
printf(“%s”,s);

getchar ();
return 0;
} 
. 3. Memset () function is used to initialize the memory space. Such as:
char STR [100];
Memset (STR, 0,100);

4. Memset () profound meaning: a section of memory space for all of a character set, generally used in the definition of strings is initialized to 'or' / 0 '; Example: char a [100]; memset (a , '/ 0', sizeof (a));

memcpy used for memory copy, copy of the object you can take it to any type of data, the data length of the copy can be specified; Example: char a [100], b [50]; memcpy (b, a, sizeof (b)); Note that as with sizeof (a), b will cause overflow memory address.

strcpy can only copy the string, it encounters '/ 0' ends copy; Example: char a [100], b [50]; strcpy (a, b); as a strcpy (b, a), to string length (the first '/ 0' before) in a note if more than 50, exceeds, will cause overflow memory address b.

5. added:
Memset easy emptying of a variable or array type configuration.

如:
struct sample_struct
{
char csName[16];
int iSeq;
int iType;
};

For variable
struct sample_strcut stTest;

In general, the method of emptying stTest:
stTest.csName [0] = '/ 0';
stTest.iSeq = 0;
stTest.iType = 0;

Very easy using memset:
memset (& stTest, 0, the sizeof (struct sample_struct));

If the array is:
struct sample_struct the TEST [10];
the
memset (TEST, 0, sizeof ( struct sample_struct) * 10);

6. strcpy
Prototype: extern char * strcpy (char * dest, char * src);
Usage: #i nclude
Function: copy indicated by the NULL-terminated character string src to the array referred to in dest.
Description: memory pointed to src and dest dest region may not overlap and must have sufficient space to accommodate the character string src.
Returns a pointer pointing to dest.
memcpy
Prototype: extern void * memcpy (void * dest, void * src, unsigned int count);
Usage: #i nclude
Function: copy count memory area indicated by the src-byte memory region referred to dest.
Description: src and dest referred memory area can not be overlapped, the function returns a pointer to a pointer dest.
memset
Prototype: extern void * memset (void * buffer, int c, int count);
Usage: #i nclude
Function: The byte buffer within the meaning of the first count memory area is set to the character c.
Description: Returns a pointer to the buffer pointer.

memcpy used for memory copy, copy of the object you can take it to any type of data, the data length of the copy can be specified.

Example: char a [100], b [50]; memcpy (b, a, sizeof (b)); as noted by sizeof (a), b will cause overflow memory address.

Strcpy can only copy the string, it encounters '/ 0' ends copies.

Example: char a [100], b [50]; strcpy (a, b); as a strcpy (b, a), to be noted that a length of the string (the first '/ 0' before) is more than 50 bits, such as more than, it will cause the memory address of b overflow.

str strncpy can also be used with the parameters (a, b, n)

========================================================

memset main application is to initialize a memory space.
memcpy is a copy source data space to a destination space.
strcpy string for Copy, encounters '/ 0' will end.

If you understand this, you should know the difference between them: for example, when you initialize a piece of space, use memcpy, then it should be how to write, is not it seem stupid.
m int [100]
Memset ((void *) m, 0x00, the sizeof (int) * 100); // Ok!
memcpy ((void *) m, "/ 0/0/0/0 ....", sizeof (int) * 100); // it's wrong.

 

 

Detailed usage memset (rpm)

memest原型 (please type “man memset” in your shell)

void *memset(void *s,   int c, size_t n);

memset: effect of filling a given value in a section of memory blocks, a method of clearing the operation of the fastest its larger structures or arrays.

Three common mistakes

First: the anti-out position c and n.

You must remember that if we want a char a [20] cleared, it must be memset (a, 0, 20) 
rather than memset (a, 20, 0)

Second: the excessive use memset, I think these programmers may have some psychological shadow, they are afraid of uninitialized memory, so they can write code like this:

char buffer[20];

memset(buffer, 0, sizeof((char)*20)); 
strcpy(buffer, “123”);

memset here is superfluous, because this memory was immediately covered, there is no clear meaning.

Third: In fact, strictly speaking, this error can not be considered wrong memset, but it often occurs in the case of using memset

int some_func(struct something *a){ 
… 
… 
memset(a, 0, sizeof(a)); 
… 
}

Q: Why do we use memset to zero memset (& Address, 0, sizeof (Address)); often see such usage, in fact, is not the case, when the distribution of data, and the remaining space will be set to zero?.

A: 1. If you do not empty, might test appear among the outliers. You do take a look at the following test 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 is not true! Especially for a character pointer type, the remaining part is usually not 0, it may make a test, the definition of an array of characters, an input string of characters and, if not memset cleared, there will be displayed using the distortion MessageBox (0 means NULL, if so, the default character ends, the output will not back garbled)

ask:

As a demo is, the array element values ​​can characters are set to 1,

#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;
}
而,如下程序想吧数组中的元素值设置成1,却是不可行的
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
     int a[5];
     memset(a,1,5);//这里改成memset(a,1,5 *sizeof(int))也是不可以的
     for(int i = 0;i < 5;i++)
       cout<<a[i]<<”   “;
     system(“pause”);
     return 0;
}
问题是:

1, Why can a program, while the second is not,
2, do not want to use for, or while loop to initialize int a [5]; can do it? (There's no one like memset () This function initializes)

answer:

1. Because the first program of the array is a character, and character occupy memory size is 1Byte, and memset function is in bytes assignment, so you do not have output problems. And the second integer is a program, or by using memset byte assignment, so that after completion of the assignment, the value of each array element is actually 0x01010101 i.e. decimal 16,843,009. You look at your output if so?

2. If using memset (a, 1,20);
that is, 20 bytes of memory pointed to a assignment, each with 1 ASCII character to fill, and changed to a binary 1 is 00000001, representing a byte. Is a 4-byte INT element, is engaged with 1000000010000000100000001, equal to 16843009, to complete the assignment of the elements of a INT.

Guess you like

Origin www.cnblogs.com/mhq-martin/p/11741575.html