Differences between static storage areas, stacks, and stacks in C ++

Source: Internet
Author: User

Learning c ++ is a very sad thing if you don't know about memory allocation. Moreover, a C ++ programmer cannot grasp the memory and understand the memory, and cannot become a qualified C ++ programmer.
I. Basic Memory Structure
The programmable memory can basically be divided into the static storage area, heap area, and stack area. Their functions are different, so they are used differently.
Static storage zone:The program has been allocated when it is compiled, and the program exists throughout the entire runtime. It stores static data, global data, and constants.
Stack zone:When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
Heap zone:It is also called dynamic memory allocation. When the program runs, it uses malloc or new to apply for memory of any size. The programmer is responsible for releasing the memory with free or delete when appropriate. The lifetime of the dynamic memory can be determined by us. If we do not release the memory, the program will only release the dynamic memory at the end. However, a good programming habit is: if a dynamic memory is no longer used, it needs to be released. Otherwise, we think there is a memory leak.

Ii. Differences between the three
We can see through the code segment what operations and differences are required for the three parts of the memory, and what should be paid attention.
Example 1: static storage and stack

Copy codeThe Code is as follows: char * p = "Hello World1 ";
Char a [] = "Hello World2 ";
P [2] = 'a ';
A [2] = 'a ';
Char * p1 = "Hello World1 ;"

This program has an error. The error occurs in p [2] = 'A'. Why, both the Variable p and variable array a exist in the stack zone (any temporary variable is in the stack zone, including the variables defined in the main () function ). However, the data "Hello World1" and "Hello World2" are stored in different regions. Because the data "Hello World2" exists in the array, this data is stored in the stack zone and there is no problem with its modification. Because the pointer Variable p can only store the address of a bucket, the data "Hello World1" is a String constant, so it is stored in the static storage area. Although p [2] can access the third data unit in the static storage area, that is, the storage unit where the character 'L' is located. However, because the data "Hello World1" is a String constant and cannot be changed, a memory error is reported when the program is running. In addition, if p and p1 are output, the addresses saved in p and p1 are identical.
Example 2: Stack and stack
Copy codeThe Code is as follows: char * f1 ()
{
Char * p = NULL;
Char;
P = return p;
}

Char * f2 ()
{
Char * p = NULL:
P = (char *) new char [4];
Return p;
}

Both functions return the address of a bucket. What is the difference between the two functions? Although the f1 () function returns a bucket, this space is a temporary space. That is to say, this space only has a short life cycle. When function f1 () is called, its life value is lost, that is, the space is released. Therefore, when the f1 () function is called, if the program contains the following statement:Copy codeThe Code is as follows: char * p;
P = f1 ();
* P = 'a ';

At this time, compilation does not report errors, but an exception error occurs when the program is running. Because you have performed operations on the memory that should not be operated (that is, the storage space that has been released. However, the f2 () function does not have any problems. Because the new command applies for a bucket in the heap. Once the application is successful, this memory will always exist unless you delete it or terminate the program. It can also be understood that the heap memory is a shared unit and can be accessed by multiple functions. If you need to return multiple data but there is no way, heap memory will be a good choice. However, you must avoid the following:Copy codeThe Code is as follows: void f ()
{
...
Char * p;
P = (char *) new char [100];
...
}

This program has done a very meaningless and harmful thing. Although heap memory is applied, p Stores the first address of heap memory. However, this variable is a temporary variable. When the function call ends, the variable p disappears. That is to say, there is no first address for storing the heap memory. We will never be able to use that heap memory. However, the heap memory is always identified by you (because the program is not finished, you have not deleted it, so the heap memory is always identified as the owner of your program), and other processes or programs cannot be used. This immoral "rogue behavior" (we don't need it, but we don't want it to be used by others) is called Memory leakage.

In short, the biggest difference between the stack, stack, and static storage is that the stack life cycle is short. However, the life cycle of the heap and static storage areas is equivalent to the same as that of the Program (if you delete the heap memory before running the program ), we convert this variable or data into a global variable or data. However, the heap memory space is more flexible, because it allows you to release it whenever you don't need it, the static storage area will remain in the entire life cycle of the program.

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.