Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the storage area of C++?

Shulou Source: shulou.com Published: 2022-06-02 09:43:05 09月21日 Update

This article introduces the knowledge of "what is the storage area of C++?". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the program development, we divide the C++ storage area into the following steps:

1, stack area (stack)-automatically allocated and released by the compiler, storing the parameter values of the function, the values of local variables, etc. It operates in a manner similar to the stack in the data structure.

2. Heap-generally allocated and released by programmers, if the programmer does not release, the program may be reclaimed by the system at the end of the program. Note that it is different from the heap in the data structure, but the allocation method is similar to the linked list.

3. Global area (static zone) (static)-the global and static variables are stored together, the initialized global variables and static variables are in the same area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. Released by the system at the end of the program

4. The text constant area-the constant string is put here. Released by the system at the end of the program

5. Program code area-the binary code that stores the body of the function.

Local variables in the C++ storage area can be initialized with any expression that matches the type.

Global variables can only be initialized with constant expressions (Constant Expression).

For example, the global variable pi is legally initialized like this:

Double pi = 3.14 + 0.0016

But this initialization is illegal:

Double pi = acos (- 1.0)

If the global variable is not initialized at definition time, the initial value is 0, and if the local variable is not initialized at definition time, the initial value is uncertain.

Therefore, the local variables in the C++ storage area must be assigned before use, if the subsequent calculation based on an uncertain value will certainly introduce Bug.

This is the end of the content of "what is the storage area of C++"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Variables regions globals programs storage caching local static constant systems assignments code content functions data data structures methods more knowledge programmers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple OPPO Reno Microsoft Redmi