C + + memset usage

Source: Internet
Author: User

The Memset function exists in the header file <cstring> of C/s + + and is used (typically) to initialize the array. (especially when an array has been used (crossed out))

Format:

memset (void *s,int ch,size_t N);
Function Explanation: Replaces the N bytes (typedef unsigned int size_t) in s that follow the current position with CH and returns S.
Memset: The function is to populate a block of memory with a given value, which is the fastest way to clear 0 operations on large structures or arrays.
--from Baidu Encyclopedia
I believe this passage can be understood by everyone. To facilitate understanding of the given example:
1#include <cstdio>2#include <cstring>3 4 intMain () {5     inta[Ten];6Memset (A,0,sizeof(a));7      for(intI=1; i<=9; i++) printf ("%d", A[i]);8     return 0;9}

Run the program and you'll see a line of cute 0 rows standing in front of you.

However, when I was in the brush (water) problem, in order to lazy write for loop I directly use Memset to initialize an array to 1, just like this:

memset (Enode,1,sizeof(enode));

That day reminded me of my fear of being dominated by WA, until I found out that enode after the memset operation in the commissioning period:

Why is it?

Originally, the Memset function initializes a block of memory by Byte, so it cannot be used to initialize an int array to a value other than 0 and-1 (unless the value is the same as high and low). (--from Baidu Encyclopedia)

As for Memset's memory management involving pointers, it is not a category that our earth people understand--even the pointers themselves are not for human use.

Come and read with me: Do not touch the hands rp++!!

C + + memset usage

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.