"Turn" C language Knowledge Collation (3): Memory management (detailed version)

Source: Internet
Author: User

Original link: http://www.cnblogs.com/yif1991/p/5049638.html

In computer systems, especially embedded systems, memory resources are very limited. Especially for mobile developers, the limitation of hardware resources makes it the most important problem in program design that how to manage memory resources effectively. This article is the author in the study of C language memory management process of a summary, if there is inappropriate, hope that readers are not hesitate to correct.

First, several basic concepts

In the C language, there are more knowledge points about memory management, such as functions, variables, scopes, pointers, etc., in the study of C language memory management mechanism, the first simple review of these basic concepts:

1. Variables: not explained. However, there are several types of variables that need to be figured out:

    • Global variables (external variables): Variables that appear outside the code block {} are global variables.
    • Local variables (automatic variables): In general, the variables defined inside the code block {} are automatic variables, or you can use auto to display the definitions.
    • Static variable: A variable that the memory location has not changed during the execution of the program and is modified with the keyword static. A static variable inside a code block can only be accessed within this block of code, and a static variable outside the code block can only be accessed by the file that defines the variable.

Note: When extern modifies a variable, it can be considered as either a definition or a declaration, depending on the case, but the extern modifier can only be defined, without ambiguity.

2. Scope: usually refers to the scope of the variable, broadly speaking, there are also function scope and file scope, and so on. I understand the scope of a thing can exist in the region or scope, such as a drop of water only in the 0-100 degrees Celsius can exist, beyond this range, the broad sense of the "waters" does not exist, it becomes ice or gas.

3. Function: Not explained.

Note: Functions in the C language are global by default, and you can use the static keyword to declare a function as a static function (a function that can only be accessed by the file that defines the function).

Two, four areas of memory

The memory in the computer is managed by the partition, the memory between the program and the program is independent, can not access each other, such as QQ and browser respectively occupy the memory area is not mutually accessible. And the memory of each program is also partition management, an application of memory can be divided into a number of areas, we need to understand the main four areas, usually called memory four areas, such as:

1. Code Area

When the program is loaded into memory by the operating system, all executable code (program code directives, constant strings, etc.) are loaded into the code area, which is invariant during the program's run. The code area is parallel and contains a bunch of instructions that cannot be changed while the program is running. Functions are also part of the code, so functions are placed in the code area, including the main function.

NOTE: "int a = 0;" Statements can be split into "int A;" and "a = 0", define the "int a" of the variable A; The statement is not code, it executes at the time of program compilation, and is not placed in the code area, only the "a = 0" sentence in the code area.

2. Static Zone

Static zones store all global variables and static variables in the program.

3. Stack area

Stack is an advanced memory structure, all the automatic variables, function parameters are stored in the stack, this action is done automatically by the compiler, we write the program does not need to consider. The stack area can be modified at any time while the program is running. Automatically pops up from the stack when an automatic variable exceeds its scope.

    • Each thread has its own stack;
    • The maximum size of stack is fixed, and the stack overflow is caused by exceeding;
    • The memory on the stack is freed automatically when the variable leaves the scope.

Talk was cheap, show you the code:

1 //Experiment One: Observe the memory address of code area, static area, and stack area2#include"stdafx.h"3 intn =0;4 voidTestintAintb)5 {6printf"the address of form parameter A is:%d\n the address of form parameter B is:%d\n", &a, &b);7 }8 int_tmain (intARGC, _tchar*argv[])9 {Ten     Static intm =0; One     intA =0; A     intb =0; -printf"the address of the automatic variable A is:%d\n the address of the automatic variable B is:%d\n", &a, &b); -printf"the address of the global variable n is:%d\n the address of the static variable m is:%d\n", &n, &m); the Test (A, b); -printf"the address of the _tmain function is:%d", &_tmain); - GetChar (); -}

The results of the operation are as follows:

Results analysis: The automatic variables A and B are defined and assigned, are stored in the stack, memory address only 12, it should be noted that the address of a is larger than B, this is because the stack is an advanced data storage structure, the first storage of a, after the storage of B, visualization such as (note address number order). Once the scope is exceeded, the variable B is destroyed before the variable A. It was like putting clothes in the box, the first thing to be taken out, and the last to be taken out.

1 //experiment two: stack variables and scopes2#include"stdafx.h"3 //The return value of the function is a pointer, although it is possible to run the program, but it is not legal because4 //to do this, you need to add the static keyword modifier before the x variable, that is, static int a = 0;5 int*Getx ()6 {7     intx =Ten;8     return&x;9 }Ten  One int_tmain (intARGC, _tchar*argv[]) A { -     int*p =getx (); -*p = -; theprintf"%d", *p); - GetChar (); -}

This code does not have any syntax errors and can get the expected result: 20. But this is problematic: because the scope of the variable x in int *p = GETX () is inside the body of the Getx () function, the address of a temporary stack variable x is obtained, and the address is invalid after the Getx () function call, but the subsequent *p = 20 is still accessing and modifying it. The results may or may not be wrong, the actual work should avoid this practice, or how to die do not know. You cannot return the address of a stack variable through the return value of a function, remember!

  In addition, the stack is not very large, usually in K units. If the large array is stored in the program directly in the function of the stack variable, it is likely that memory overflow, resulting in a program crash (the following experiment three), strictly speaking, should be called stack overflow (when the stack space is full, but also to the stack memory pressure variable, this is called Stack overflow).

1 //experiment Three: see what is Stack overflow2 int_tmain (intARGC, _tchar*argv[])3 {4     Chararray_char[1024x768*1024x768*1024x768] = {0};5array_char[0] ='a';6printf"%s", Array_char);7 GetChar ();8}

What to do? It's time for the pile.

4. Heap Area

Heap, like a stack, is a memory area that can be modified at any time while the program is running, but not in the same order as the stack. More importantly, the heap is a large container, its capacity is much larger than the stack, which can solve the above experiment three caused by the memory overflow difficulties. Generally more complex data types are placed in the heap. However, in the C language, the application and release of heap memory space need to be done manually through code. For a 32-bit operating system, the maximum management of 4G memory, where 1 G is for the operating system itself, the remaining 3G is to the user program, a user program can theoretically use 3G of memory space. The memory on the heap must be released manually (c + +) unless the language execution environment supports GC (such as C # running on. NET has a garbage collection mechanism). How does that heap of memory work?

Next look at the allocation and release of heap memory:

malloc and free

The malloc function is used to allocate memory of the specified size in the heap, in bytes (byte), and the function returns a void * pointer; Free is responsible for releasing malloc allocated memory in the heap. malloc and free must be used in pairs . Look at the following example:

1 //experiment Four: Solve the stack overflow problem2#include"stdafx.h"3#include"stdlib.h"4#include"string.h"5 6 voidPrint_array (Char*p,CharN)7 {8     inti =0;9      for(i =0; I < n; i++)Ten     { Oneprintf"p[%d] =%d\n", I, P[i]); A     } - } -  the int_tmain (intARGC, _tchar*argv[]) - { -     Char*p = (Char*)malloc(1024x768*1024x768*1024x768);//requested memory in the heap -Memset (P,'a',sizeof(int) *Ten);//Initializing Memory +     inti =0; -      for(i =0; I <Ten; i++) +     { AP[i] = i + $; at     } -Print_array (P,Ten); -      Free(p);//frees the requested heap memory - GetChar (); -}

The result of the operation is:

The program can run normally, which solves the stack overflow problem of experiment three just now. What is the capacity of the heap? In theory, it can use all the space except the system takes up memory space. Actually smaller than this, for example, we usually open such as QQ, browser and other software, but this is generally sufficient. In experiment two, we can't return the address of a stack variable through the return value of a function, what if we need to return the address of a variable defined within a function? You can do this:

1 //Experiment Five:2#include"stdafx.h"3#include"stdlib.h"4 5 int*Getx ()6 {7     int*p = (int*)malloc(sizeof(int));//applied for a heap of space8     returnp;9 }Ten  One int_tmain (intARGC, _tchar*argv[]) A { -     int*PP =getx (); -*PP =Ten; the      Free(PP); -}

It is no problem to write this, you can return a heap address through the function, but remember to use the free function to release the requested heap memory space . "Int *p = (int *) malloc (sizeof (int));" It is also valid to replace "static int a = 0". Because the static zone's memory is valid for the entire duration of the program's operation, the free function behind it is not available!

The functions used to request memory space in the heap are also calloc and realloc, similar in usage to malloc.

Iii. Case studies Case study one

Some analyses are as follows:

The main function and Updatecounter are part of the code, so they are stored in the code area.

Array a defaults to global variables, so it is stored in the static zone

The "char *b = NULL" In the main function defines the automatic variable b (variable), so it is stored in the stack area

Then "b = (char *) malloc (1024*sizeof (char));" Part of the memory space is applied to the heap, so this space is in the heap area

Case Two

The following points need to be noted:

    • The stack is increased from high address to low address direction;
    • in C language, the order of the function parameters is right to left, so the 3 parameters of the Updatecounter function are A1, C, b;
    • The value is passed between the formal parameter and the actual parameter in C language. The parameters in the Updatecounter function a[1], C, B and static zone a[1], C, B are not the same ;

"Char *b = NULL" Defines the address of a pointer variable b,b is 0XFFF8, and the value is empty--run to "B = (char*) malloc (1024*sizeof (char))" When a piece of memory is applied to the heap (assuming the memory address is 0x77a0080) to B, at which point the address of B does not change, but its value becomes 0x77a0080, which points to the address of a heap space (the value of the stack variable points to the heap space), and the memory change of this process B is as follows:

---------->

Iv. Learning the purpose of memory management

Learning memory management is to know how to manage our memory at the right time in the future. So that's the problem? When do I use stacks with heaps? The following three principles are generally followed:

  • If you know exactly how much memory the data occupies, then use the stack when the data is small, and the heap when it is large;
  • If you do not know the size of the data (it may take a large amount of memory), it is best to use the heap (because of this insurance);
  • If you need to create an array dynamically, use the heap.
  • 1 //experiment Six: Creating arrays dynamically2 int_tmain (intARGC, _tchar*argv[])3 {4     inti;5scanf"%d", &i);6     int*array = (int*)malloc(sizeof(int) *i);7     //...//Here are other things to do with dynamically created arrays8      Free(array);9}
    The last of the last

    When the operating system manages memory, the smallest unit is not a byte, but a memory page (a 32-bit operating system's memory page is typically 4K). For example, the initial application for 1K of memory, the operating system will allocate 1 memory pages, that is, 4K of memory. 4K is a compromise choice, because: the larger the memory page, the more wasted memory, but the operating system memory scheduling is high efficiency, do not have to allocate and free memory frequently, the smaller the memory page, the less memory waste, but the operating system memory scheduling is inefficient, need to allocate and release memory frequently . Embedded system memory memory resources are scarce, and its memory pages will be smaller, so in the embedded development requires special attention.

"Turn" C language Knowledge Collation (3): Memory management (detailed version)

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.