Windows client/C + + programming Specification "Recommendations"--variables and constants

Source: Internet
Author: User

8 variables and Constants 8.1 Do not use global variable levels as much as possible: "Request"
Description: Abuse of global variables, like the misuse of Goto, is a disaster. It makes logic harder to debug and control.

8.2 Global variables that do not involve external use need to be decorated with the static keyword: "Request"
Description: This avoids conflicts.

8.3 variables need to be initialized before they can be used: "must"

Description: A variable is best initialized when defined. If the definition cannot be initialized, it should be initialized immediately after the definition.

Let's analyze the scenario in which the following variables are not initialized and are used:

In the debug environment, we declare a variable where the compiler uses 0xCC to populate the variable space. In the release environment, the variable space is not initialized. So we often find strange phenomena: logic is correct in the debug environment, logic is wrong in release environment.
A variable that does not initialize has the following hazards:

    1. Produce dirty data. Because the variable space is not initialized, it causes dirty data to be generated. You can see the following example.
    2. Affect normal logic. Dirty data will cause logic errors where the relevant data is later used.
    3. Indirectly causes a crash. You can see the following example:

void myprintf (char* p) {strcat (P, "abcdefghijklmnopqrstuvwxyz"); printf ("%s", p);} int _tmain (int argc, _tchar* argv[]) {char szbuffer[32];  std::string str = "01234"; memcpy_s (Szbuffer, _countof (szbuffer), Str.c_str (), str.length ());  myprintf (szbuffer); return 0;}

In this example, a 32-byte space is allocated on the stack. Because the space is not initialized, 0x00 is not present in this memory for a certain probability. The subsequent strcat will cause a stack overflow. I'm going to put strcat inside a function for a more convenient trigger crash.


After executing the strcat.



Based on the above hazards, we need to initialize the variables when declaring them. The member variables of the class, which need to be initialized in the constructor.

Here's a special note for static array initialization, using:

Char Szpath[max_path] = {0};
The following writing is not easy to write the above and high efficiency:
Char Szpath[max_path];  memset (szpath, 0, sizeof (szpath));

8.4 Rows Define only one variable level: "Request"
Description: A row defines only one variable, which makes it easier to initialize and maintain variables.

8.5 Do not use constants to participate in the operation level directly: "must"
Description: Using constants directly in code logic will cause code logic to be very difficult to read. Because constants do not have a ideographic nature.
Example:
RECT Mainrc;mainrc.left + = 124;mainrc.top + = 56;
This example attempts to calculate the position of a control in a window (windowless control). But for the first time to read such a code, who knows what those two constants mean? You can modify this:
RECT mainrc;const unsigned int unclosebtnoffx = 124;const unsigned int unclosebtnoffy = 56;mainrc.left + = Unclosebtnoffx;m Ainrc.top + = Unclosebtnoffy;

The 8.6 variable is defined at a level close to the first use (except C): "Request"

Description: This design will make the code more readable.


(reprint please indicate CSDN blog for breaksoftware)

Windows client/C + + programming Specification "Recommendations"--variables and constants

Related Article

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.