DS Plan C deep learning project: [summary] [variable] storage region of the Variable

Source: Internet
Author: User

To join this plan:

1,
Method 1
: Join
Qq
GROUP:

93684322
.

2,
Method 2
: Join
Csdn
GROUP:

DS plan



.

1.1 example of a variable storage domain 1.1.1

Pang123hui first provides an example of the learning code circulating on the Internet:

Int A = 0; // global Zone

Void main ()

{

Int B; // Stack

Char s [] = "ABC"; // s in the stack, ABC in the text Constant Area

Char * P1, * P2; // Stack

Char * P3 = "123456"; // 123456 in the constant zone, and P3 in the stack

Static int C = 0; // global Zone

P1 = (char *) malloc (10); // P1 is on the stack, and the allocated 10 bytes are on the heap

P2 = (char *) malloc (20); // P2 on the stack, 20 bytes allocated in the heap

Strcpy (P1, "123456"); // place 123456 in the constant area

}

In this code example, words such as "Global zone", "stack", "text constant zone", and "Heap" are displayed. For unification, we use the statement in Expert C: Stack segment, BSS segment, data segment, and text segment.

The functions of each segment are as follows:

1. Text Segment: Contains program instructions, which are generally not changed during program execution.

2. Data Segment: including initialized global and static variables and their values.

3. BSS segment: Contains uninitialized global and static variables.

4. Stack segment: contains the local variables declared inside the function.

Of course, the role of the above section is not only described here. The specific role will be described in the following knowledge points.

1.1.2 test the storage location of variables through code

In Linux, you can run the "size" command to view the size of each segment of the executable program. However, the segment structure in the executable program is not exactly the same as that in the memory of the running program, but there is a certain ing relationship. As shown in (picture information comes from expert C Programming):

The following code example and "size" are used to study the storage area of variables.

Test. c

Int main ()

{

Return 1;

}

Compile and view the size of each segment of the executable program:

Change test. C:

Int g_data;

Int main ()

{

Return 1;

}

Compile and view the size of each segment of the executable program:

It can be found that the text and data segments are not sent, while the BSS segment is added by four bytes.

Conclusion 1: uninitialized global variables are stored in the BSS segment.

Continue:

Int g_data = 1;

Int main ()

{

Return 1;

}

Compile:

It can be found that the BSS segment is the same as the text segment, and the data segment is increased by 4 bytes.

Conclusion 2: initialized global variables are stored in the data segment.

Continue:

Int main ()

{

Static int g_data;

Return 1;

}

Compile:

It can be found that the text and data segments are not sent, while the BSS segment is added by four bytes.

Conclusion 3: uninitialized static variables are stored in the BSS segment.

Continue:

Int main ()

{

Static int g_data = 1;

Return 1;

}

Compile:

It can be found that the BSS segment is the same as the text segment, and the data segment is increased by 4 bytes.

Conclusion 4: initialized static variables are stored in the data segment.

Continue:

Int main ()

{

Int I _data = 1;

Return 1;

}

Compile:

It can be found that the BSS segment is the same as the data segment, while the text segment is increased by 16 bytes. Local variables are generated in the stack segment during execution and released after the function is executed.

Conclusion 5: The local variables declared in the function are stored in the stack segment.

Continue:

Const int g_data = 1;

Int main ()

{

Return 1;

}

Compile:

After defining the global variable as "const", you may be surprised that the BSS and data segments have not changed, but the text segments have increased by four bytes.

Conclusion 6: The global variables modified by const are stored in the text segment.

So, what is the local variable of const?

Continue:

Int main ()

{

Const int I _data = 1;

Return 1;

}

Compile:

Conclusion 7: The local variables modified by const are stored in the stack segment.

Continue:

Char * pstr = "";

Int main ()

{

Return 1;

}

Compile:

Make the following changes:

Char x pstr = "123456789 ";

Int main ()

{

Return 1;

}

Compile:

It can be found that the size of the front and back data segments and BSS segments have not changed, but the size of the text segments has increased by 9 bytes.

Conclusion 8: string constants are stored in text segments.

1.1.3 conclusion

1. initialized global variables and static variables are stored in the data segment.

2. uninitialized global variables and static variables are stored in the BSS segment.

3. The local variables declared in the function are stored in the stack segment.

4. The global variables modified by const are saved in the text segment, and the local variables modified by const are saved in the stack segment.

5. string constants are stored in text segments.

1.1.4 extended reading

Chapter 2 of C expert programming-detailed descriptions of the functions of each segment.

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.