Use of the memset () function

Source: Internet
Author: User

The first time I came into contact with the memset () function. It is indeed a fancy way that can efficiently implement one-time initialization of an array. The following is an introduction:

The memset () function is defined in the header file <cstring>. prototype is as follows:

void * memset ( void * ptr, int ch, size_t num );

The function is used to: Fill the first num bytes (note that it is not an element) of block of memory (pointed by the NULL pointer PTR) with value. Finally, we will explain the meaning of this sentence.

The size_t type is defined in the C standard library.

Baidu encyclopedia is quoted as saying:

In C ++, size_t is designed to adapt to multiple platforms. The introduction of size_t enhances the portability of programs on different platforms. Size_t is a type of data customized for the system, generally an integer (INT), because the C/C ++ standard only defines a minimum number of digits, rather than a required fixed number of digits. In addition, in the memory, the logarithm of the high alignment storage or low alignment storage systems are different. To improve code portability, it is necessary to define such a data type. Generally, this type defines the memory occupied by it. Of course, some are defined by the compiler or system. The test shows that in a 32-bit system, size_t is 4 bytes, while in a 64-bit system, size_t is 8 bytes. This type can enhance the program portability.

Therefore, size_t generally occupies the same storage space as INT:

#include <iostream>using namespace std;int main(){    cout << sizeof(size_t) << endl;    cout << sizeof(int) << endl;    return 0;}

The running result is as follows:


It is not hard to see that for (my computer is 32-bit), the int and size_t spaces are the same.

In order to further strengthen the concept of memory, I made the following Transformation tests:

#include <iostream>using namespace std;int main(){    int i = 100;    cout << sizeof(static_cast<char>(i));    return 0;}

The running result is as follows:

It can be seen that during the transformation, the transformed data is stored in a temp char variable. Of course, the occupied memory is 1 byte.

Let's look at the following example:

#include <iostream>using namespace std;int main(){    int i = 100;    cout << sizeof(static_cast<char>(i)) << endl;    cout << sizeof(i);    return 0;}




However, information may be lost during the transformation.



The following uses the memset () function.

In the official file, comments for the memset () function are as follows:

. Is a very efficient way to set all values of the_array to zero.

First, this function returns the initialized buffer.


You can view the memset () function to initialize the array and use for loop to compare the time required for Array initialization:



It can be seen that as the size of the array increases, the efficiency advantage of memset becomes more and more obvious.

The above is initialization in the form of char. We can directly use the size as the third parameter of memset. Because the space occupied by char arrays (measured in bytes) is the size value.

However, for an array of the int type, because the value of an int type occupies 4 bytes, it is used differently.

For details, see the following example:

 

#include <iostream>using namespace std;/* memset example */#include <iostream>#include <cstring>int main (){    int myArray[11];    char str[] = "almost every programmer should know memset!";    memset(str,'-',6);    cout << str << endl;    memset(myArray, 10, 11 * sizeof(int));    for(int i = 0; i < 11; i++) {       cout << myArray[i] << " ";    }    cout << endl;    memset(myArray, 10, sizeof(myArray));    for(int i = 0; i < 11; i++) {       cout << myArray[i] << " ";    }    return 0;}
The running result is as follows:



Problem: in the preceding example, the output of the char array Initialization is as expected. However, the integer array does not meet the expectation. Why. The answer is that memset assigns a value of one byte to the memory block. Because an int element in an int array has four bytes, it is not as expected.

I believe it can be changed to 0, and it can be changed to-1:

# Include <iostream> using namespace STD;/* memset example */# include <iostream> # include <cstring> int main () {int myarray [11]; char STR [] = "almost every programmer shocould know memset! "; Memset (STR, '-', 6); cout <STR <Endl; memset (myarray, 0, 11 * sizeof (INT )); // note that it cannot be <span style = "font-family: Arial, Helvetica, sans-serif;"> memset (myarray, 0, 11 ); ah </span> for (INT I = 0; I <11; I ++) {cout <myarray [I] <"" ;}cout <Endl; memset (myarray,-1, sizeof (myarray); For (Int J = 0; j <11; j ++) {cout <myarray [J] <";}return 0 ;}

The running result is as follows:


Therefore, the memset () function does not allow us to initialize a specific integer for the array. Anyway, you can efficiently initialize to 0 or-1 to okay.






Use of the memset () function

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.