Bzero and memset

Source: Internet
Author: User
Tags posix

Bzero:
Prototype: void bzero (void * s, int N );
Function: set the first n Bytes of the byte string s to zero and include '\ 0 '.
Note: bzero has no return value, and strings is used. h header file, strings. h was once part of the POSIX standard, but in the POSIX.1-2001 standard, these functions are marked as legacy functions and are not recommended.
These functions are no longer in the POSIX.1-2008 standard. We recommend that you use memset instead of bzero.

Memset:
Void * memset (void * s, int CH, size_t N );
Function explanation: Replace the first n Bytes in S with CH and return S;
Memset: fills in a given value in a memory block. It is the fastest way to perform the clearing operation on a large struct or array.

Three Common Errors:
First, the location of CH and N is reversed. 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, over memset, I think these programmers may have some psychological shadows. They are afraid of uninitialized memory, so they will write such code:
Char buffer [20]; memset (buffer, 0, sizeof (char) * 20); strcpy (buffer, "123 ");
The memset here is redundant, because the memory will be overwritten immediately, and clearing is meaningless.

Third: in fact, this error cannot be regarded as a memset error, but it often appears when memset is used;

Conclusion: for the initialization of character arrays, bzero and memset have no significant difference in efficiency. However, in the stress test of the project, the performance is obviously decreased, it becomes suspicious to initialize several temporary character arrays in the Program (using bzero. Therefore, the initialization method with the first character set to zero instead of all set to zero is modified, and the response is significantly improved. Originally, bzero is required to initialize the temporary array for each result retrieved by MP3. Each request requires 30 bzero to set zero for the temporary array. As a result, if it is not necessary, only the initialization method of the first (or first few) character of the temporary array is compared and bzero is used, the performance can be significantly improved.

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.