Do not use memset in disorder

Source: Internet
Author: User

Memset should be used to initialize some simple pure data structures, such:

1, array, int A [100], float B [200]

2. struct composed of simple data types:

Struct

{

Int;

Float B;

Double C [100]

}

3. A single simple data can also be used.

Int;

Memset (& A, 0, sizeof ())

Memset should not be used for initialization for arrays composed of classes, but should be initialized in the class constructor.

For example, the cstring class of MFC

Cstring;

Cstring B = _ T ("ABC ");

Memset (& A, 0, sizeof ());

A = B; // sorry, an error occurred while running.

You used memset to destroy everything in.

Similarly, cstring STR [100] should not be initialized using memset.

The cstring constructor has already set the initial value to an empty string, saving memset.

If the class is defined by yourself, initialize it in the constructor instead of calling 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.