C -45- zero-based video memory partitions: global area, stack area, stack

The near future, we want to start learning dynamic and freeing memory in C. Before learning them, we first learn program memory partitions to grasp and understand the dynamics applications, the release of memory do the basic groundwork.

Why have a program memory partitions

In general, our houses will be divided into multiple zones (bedroom, kitchen, balcony, bathroom, etc.), different regions have different functions.

Running, program partition is the same reason, it is easy to operate system management procedures, different areas of memory, different functions.

Run-time memory partition program

As a beginner, we simply know, the program has the overall area , stack area and heap area can be.

  • Global Area: Before the main function has been assigned to perform well, the program's code (machine code), global variables are global area
  • Stack Area: With the function call and return, will be automatically assigned and recycling. Relatively limited stack space (M grade).

The following code can be shown that the stack space is limited:

#include <stdio.h>

int main(int argc, char* argv[])
{
    char szBuff[10000000];
 return 0;
}

Heap

Regardless of the global memory area or stack area, a small part of the memory they usually can only use a lot of memory controlled by the operating system, dynamically allocated on demand.
At this time, the operating system is similar to the role of the hotel, and the memory is the room.
We apply dynamic memory operation, similar to the pre-assigned hotel reservations;
we release the memory of dynamic action, similar to the check-out.
Rooms have been there that we can use, depending on whether the hotel licensed to us (leased to us).
Memory is similar, the memory has always been there, but whether we can use the program, using the piece of memory address, depending on how the operating system assigned to us.

Described above, the operating system is dynamically controlled allocation management, memory recovery region, called the heap .

After we will learn how to use the C standard library functions malloc and free, dynamic application and release the heap memory.

Guess you like

Origin www.cnblogs.com/shellmad/p/11695694.html