Default initialization rules for static local variables

Source: Internet
Author: User

On page 88th of Windows program design, there is an API function call (In addition, for the Program on this page-"SYSMETS added to the scroll bar", this link provides a good comment and operation, worth a look: http://blog.csdn.net/sergery/article/details/8029796 ):

SetScrollPos (hwnd, SB_VERT, iVscrollPos, TRUE );

Among them, iVscrollPos is a static variable defined in WndProc in the window process. If the initial value is not given, the default initialization rule is used, and relevant rules are found for review. The summary is as follows:

1. Make sure that the static local object is initialized no later than when the program executes the process for the first time after the Definition Statement of this object. Once this object is created, it will not be undone before the end of the program (the following 2nd articles provide a better explanation of this sentence ). -- Page 345 of C ++ Primer

2. Assigning an initial value to a static local variable is performed during compilation, that is, only the initial value is assigned once, and it has an initial value when the program is running. In the future, the initial values will not be re-assigned for each function call, but the values at the end of the last function call will be retained. However, assigning an initial value to an automatic variable is not performed during compilation, but during function calling. Each call to a function returns a value, which is relative to executing a value assignment statement.

3. If initial values are not assigned when defining local variables, the initial values 0 (For numeric variables) or null characters (For numeric variables) are automatically assigned to static local variables during compilation ). For an automatic variable, if the initial value is not assigned, its value is uncertain. This is because the storage unit has been released after each function call, and the storage unit is re-allocated next time, and the value in the allocated unit is uncertain. -- Page 141 of C ++ programming (fourth edition of Tan haoqiang), which contains other relevant rules.

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.