The difference between global variables, ordinary local variables, and static local variables in C + +

Source: Internet
Author: User

In C + +, it is often difficult to tell the difference between global variables, local variables, and static local variables. This blog distinguishes these three variables from the variable storage attribute and the scope of the identifier at two angles.

First, let's look at how the memory area of a C + + program is allocated.

From that, we can see that the memory area of C + + program is divided into code area, global data area, heap area and stack area . Where global variables and static local variables are stored in the global data area, these two variables have been allocated and initialized at the beginning of the program. Ordinary local variables are stored in the stack, which is generated when the program enters the declared code block and is deleted when the code block is closed.

Secondly, from the perspective of the scope of identifiers, we can assume that:

1. Global variables and static local variables have a file scope ;

2. Ordinary local variables have function scope or block scope .

The difference between global variables, ordinary local variables, and static local variables in C + +

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.