Memory Allocation (heap, stack, BSS, code segment, data segment)

Source: Internet
Author: User

These two days I watched the video of Jack Ma's instructor and mentioned the 10 thousand key in the video. I learned how to operate the memory in the program running. I mainly talked about the heap, stack, data, and it was a bit dizzy. I read it twice, so I can understand it a little bit.


Introduction

When the program is running, it is stored in the memory. It is divided into five parts based on static, member functions, code segments, objects, and so on.

1 Stack

2 heap

3 BSS segment-Global zone-(static zone)

4. Code segment

5 Data segments


Stack

Stores local variables, temporary variables, declarations, return values, and addresses (pointers) pointing to heap objects) in short, store some small things, when not needed, the stack will be automatically cleared, such as an addition method, declare two int and assign values, these two are placed in the stack, Class the constant type of the eight basic variables, the declared values will be in the stack, and the others will be placed in the heap. Note: (byte, short, Int, long, Char, float, double, Boolean) (I heard that it is similar to the data structure stack, but I actually thought of this stack when I learned the data structure)


Heap

New objects are stored, and all objects in the stack are pointed to in the heap. If the pointer pointing to the heap in the stack is deleted, the objects in the heap need to be released (C ++ needs manually release) of course, our object-oriented programs now all have a 'garbage collection mechanism 'and will regularly clear useless objects in the heap.



Code segment

A piece of memory area that stores the code executed by the program. The binary code and the memory area are usually read-only. Some architectures also allow the code segment to be writable, it is actually the place where the code framework, or function body and code body are stored.



BSS-Global zone-(static zone)

The above are the three names-, the found, and similar meanings. The BSS segment is the global variables that are stored as initialization and the static variables that are not initialized.



Data Segment

The BSS segment is uninitialized. This is like taking his class and Data Segment to store initialized global and static variables.



Data

BSS + data segments, including these two segments called data zones ...... In fact, I am not very familiar with it, probably the BSS + data segment is called the data area, just like burning + meat = burning meat ......




Mind Map



Example Program

 

This is written by a senior. It is very detailed.

 

<Span style = "font-size: 18px;"> // main. CPP int A = 0; // data segment char * P1; // BSS segment main () {int B; // stack char s [] = "ABC "; // stack char * P2; // stack char * P3 = "123456"; // 123456 \ 0 is in the constant zone, and P3 is in the stack. Static int C = 0; // Class C1 = new class () in the BSS segment; // The new object is in the heap zone strcpy (P1, "123456 "); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it with the "123456" pointed to by P3. } </Span>



Summary:

The horse soldiers said that this is the omnipotent key. I still don't know much about it. I used to only know the heap and stack. Later I seduce so much, but I still had some gains. I didn't want to summarize it because I felt like I knew it. I thought about it very little. I thought about it later, and I had to summarize it because I knew little about it. How can I know more about it?




----------- You may find a fault, find a wrong article, and pick it up here ............ ------------


----------- Chenchen -----------




References

Http://blog.csdn.net/u010191034/article/details/39108635


Memory Allocation (heap, stack, BSS, code segment, data segment)

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.