C Language Memory four areas

Source: Internet
Author: User

According to the old version of the operating system to learn, memory for the program is divided into four areas. The code area, static area, stack, heap, respectively.

Results from the above program implementation:

It looks like the result is a static code stack

The static zone holds the values of all static variables and constants in the program. The size of the static area is fixed after the program is loaded into memory and will no longer change.

The code area holds the code in the program, cannot modify its value, and can only use the function by pointer or variable name. "Of course, forced modification is allowed but an unknown error may occur. 】

The heap is a large area, usually the size of 4G , (this number is usually virtual, but can use the modern operating system to use the hard disk to simulate such a space, because we casually open a few programs this will be the memory of the dead. So this 4G is a virtual situation, we are programmed to consider this is a big 4G space on it, and then to remember their allotted space in time to return. )

Stack, the stack for different operating systems, its size is not fixed, there are large and small, usually in the 4k or about 8k ,64k on the sky. But the difference is: the stack for a thread, there is a stack, if a process has 4 threads, then this thread area has 4 stacks.

For the entire allocation process, you can see that we are from the high point of the address of the allocation of static area, code area, heap, stack, so you can imagine: for every thing inside, distribution is from top to bottom

Based on this result, we analyze the allocation relationship for memory:

Heap here is not very dare to say, but for the stack, advanced out, from the bottom into the bottom out.

And its release is in the form of a code block, according to the way to obtain the address of a specific number of values.

Our verification results are basically consistent with this diagram, except that the static and code areas are somewhat different. The top of the stack is the bottom of the stack, and we write the data in a row and then release the data. The heap is constantly opening up space for use. Think of what we said before. In Windows memory using the big-endian alignment, take our program's high-level to memory low. For some large servers, the system is small-aligned. Get the low of the program to the low memory.

Now that we know the allocation of memory, we want the code we write to use as little memory as possible.

So at the time of programming: Write this: char[9]. Total sensation than Char[10] to save a byte. So is that really so?

Then ran a code like this:

The result of my system is that it will increase by 4k each time.

According to the video explanation of the idea is: when our program inmalloc, will open up32ksize of a piece of space, every32kIt has4kThe size of the memory page, we applied for a memory, first lilt the first4kThe size of the memory page so that when the current memory page is exhausted, the32kspace to take the next4kmemory page until the current32kRun out of time, go to the operating system to apply for the next piece32ksize of memory. So the loop, so essentially how much of a byte we're going to go, is a bit unnecessary. But the more important point is that we are running out of time tomallocthe heap in time to get Freeoff. More reliable.

C Language Memory four areas

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.