Memory partition; memory allocation; Concept Analysis of heap and stack; Data Structure and program example of dynamic memory management;

Source: Internet
Author: User

Note: This article is excerpted from the Internet. For future convenience, this article is dedicated. Supports originality.

 

I. In c, there are several storage areas
1. Stack-automatically allocated and released by the compiler
2. Heap-generally released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program.
3. in the global zone (static zone), global variables and static variables are stored in one partition, and initialized global variables and static variables are stored in one partition, uninitialized global variables and uninitialized static variables are in another adjacent area. -Release after the program ends
4. There is also a special place to place constants. -Release after the program ends

The variables defined in the function body are usually on the stack. The memory allocated by using malloc, calloc, realloc, and other functions is on the stack. All functions define a global volume in vitro. After the static modifier is added, all functions are stored in the global zone (static zone) no matter where they are located ), static variables defined by all functions in vitro are valid in this file and cannot be used in other files. Static variables defined in the function body are valid only in this function. In addition, strings such as "adgfdf" in the function are stored in the constant area. For example:

Int A = 0; // global initialization Zone
Char * P1; // not initialized globally
Void main ()
{
Int B; // Stack
Char s [] = "ABC"; // Stack
Char * P2; // Stack
Char * P3 = "123456"; // 123456 {post. Content} is in the constant area, and P3 is on the stack.
Static int C = 0; // global (static) initialization Zone
P1 = (char *) malloc (10); // The allocated 10-byte area is located in the heap area.
P2 = (char *) malloc (20); // The allocated 20-byte area is located in the heap area.
Strcpy (P1, "123456 ");
// 123456 {post. Content} is placed in the constant area, and the compiler may optimize it with "123456" pointed to by P3.
}

2. In C ++, the memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage.
1. Stack is the storage area for variables allocated by the compiler when needed and automatically identified when not needed. The variables are usually local variables and function parameters.
2. Heap refers to the memory blocks allocated by new. Their release compiler is not controlled and controlled by our application. Generally, a new compiler corresponds to a Delete. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.
3. The free storage zone is the memory blocks allocated by malloc and so on. It is very similar to the heap, but it uses free to end its own life.
4. in the global/static storage area, global variables and static variables are allocated to the same memory. In the previous C language, global variables were divided into initialized and uninitialized ones, in C ++, there is no such distinction. They share the same memory zone.
5. Constant storage area. This is a special storage area where constants are stored and cannot be modified. (Of course, you can modify them by improper means)

Iii. Relationship and difference between stack and stack
Specifically, modern computers (serial execution mechanisms) directly support the stack data structure at the bottom of the Code. This is reflected in the fact that there are dedicated registers pointing to the address of the stack, and dedicated machine commands to complete the operations of data in and out of the stack. This mechanism is characterized by high efficiency and limited data types supported by systems such as integers, pointers, and floating point numbers. It does not directly support other data structures. Due to the characteristics of stack, the use of stack is very frequent in the program. The call to the subroutine is done directly using the stack. The call command of the machine implicitly pushes the return address into the stack, and then jumps to the subprogram address. The RET command in the subprogram implicitly pops up the return address and jumps from the stack. The automatic variables in C/C ++ are examples of direct use of stacks, which is why the automatic variables of the function become invalid when the function returns.

Unlike the stack, the stack data structure is not supported by the system (whether a machine system or an operating system), but provided by the function library. The basic malloc/realloc/free function maintains an internal heap data structure. When the program uses these functions to obtain new memory space, this function first tries to find available memory space from the internal heap, if there is no available memory space, the system calls are used to dynamically increase the memory size of the program data segment. The newly allocated space is first organized into the internal heap and then returned to the caller in an appropriate form. When the program releases the allocated memory space, this memory space is returned to the internal Heap Structure and may be processed properly (for example, merged into a larger idle space with other idle space ), it is more suitable for the next memory allocation application. This complex allocation mechanism is actually equivalent to a buffer pool (Cache) for memory allocation. There are several reasons for using this mechanism:
1. system calls may not support memory allocation of any size. Some system calls only support fixed memory requests and their multiples (allocated by PAGE). This will cause a waste for a large number of small memory categories.
2. System Call memory application may be expensive. System calls may involve switching between user and core states.
3. Unmanaged memory allocation can easily cause memory fragmentation when a large amount of complex memory is allocated and released.

Stack and stack comparison
From the above knowledge, we can see that stack is a function provided by the system, featuring fast and efficient. Its disadvantage is its limitation and data is not flexible. Stack is a function provided by the function library, featuring flexibility and convenience, data is widely adapted, but the efficiency is reduced. The stack is the system data structure, which is unique for processes/Threads. The heap is the internal data structure of the function library, which is not necessarily unique. Memory allocated by different heaps cannot be operated on each other. Stack space is divided into static allocation and dynamic allocation. Static allocation is completed by the compiler, such as automatic variable allocation. Dynamic Allocation is completed by the alloca function. The stack does not need to be released dynamically (automatically), so there is no release function. For the sake of portable programs, dynamic stack allocation is not encouraged! Heap space allocation is always dynamic. Although all data spaces are released back to the system at the end of the program, precise memory application/release matching is the basic element of a good program.

1. fragmentation problem: for the heap, frequent new/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue. They are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack, before the pop-up, the post-stack content on him has been popped up. For details, see> data structure. We will not discuss it one by one here.
2. growth direction: For the stack, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.
3. allocation method: the heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.
4. allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated. For example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.

Clearly differentiate stack and stack:
On the bbs, the distinction between heap and stack seems to be an eternal topic. It can be seen that beginners are often confused about this, so I decided to take him first.
First, let's take an example:

Void f ()
{
Int * p = new int [5];
}

This short sentence contains the heap and stack. When we see new, we should first think that we allocated a heap memory. What about the pointer p? It allocates a stack memory, so this sentence means that the stack memory stores a pointer p pointing to a heap memory. The program will first determine the size of memory allocated in the heap, then call operator new to allocate the memory, then return the first address of the memory, and put it into the stack, the assembly code in VC6 is as follows:
00401028 push 14 h
0040102A call operator new (00401060)
0040102F add esp, 4
00401032 mov dword ptr [ebp-8], eax
00401035 mov eax, dword ptr [ebp-8]
00401038 mov dword ptr [ebp-4], eax
Here, we have not released the memory for simplicity, So how should we release it? Is it delete p? Australia, the error should be "delete [] p" to tell the compiler: I deleted an array and VC6 will release the memory based on the Cookie information.
Well, let's go back to our topic: What is the difference between stack and stack?
The main differences are as follows:
1. Different management methods;
2. Different space sizes;
3. Whether fragments can be generated is different;
4. Different Growth directions;
5. Different allocation methods;
6. Different Allocation Efficiency;
Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by programmers and memory leak is easily generated.
Space size: Generally, in a 32-bit system, the heap memory can reach 4 GB. From this perspective, there is almost no limit on the heap memory. But for the stack, there is usually a certain amount of space. For example, under VC6, the default stack space is 1 MB (as if so, I cannot remember ). Of course, we can modify:
Open the Project and choose Project> Setting> Link, select Output from Category, and set the maximum value and commit of the stack in Reserve.
Note: The minimum reserve value is 4 Byte. commit is retained in the page file of the virtual memory. Compared with the general setting, commit makes the stack open up a large value, memory overhead and startup time may be increased.
Compared with the stack, the use of a large number of new/delete operations may easily cause a large amount of memory fragments. Due to the absence of dedicated system support, the efficiency is very low; because it may lead to switching between the user State and the core state, the memory application will become more expensive. Therefore, stacks are the most widely used in applications. Even function calls are completed using stacks. The parameters and return addresses in the function call process are as follows, both EBP and local variables are stored in stacks. Therefore, we recommend that you use stacks instead of stacks.

Comparison of access efficiency:
Code:

Char s1 [] = "aaaaaaaaaaaaa ";
Char * s2 = "bbbbbbbbbbbbbbbbb ";

Aaaaaaaaaaa is assigned a value at the runtime (on the stack );
Bbbbbbbbbbbbb is determined at compilation (on the heap );
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:

Void main ()
{
Char a = 1;
Char c [] = "1234567890 ";
Char * p = "1234567890 ";
A = c [1];
A = P [1];
Return;
}

Corresponding assembly code
10: A = C [1];
00401067 8A 4D F1 mov Cl, byte PTR [ebp-0Fh]
0040106a 88 4D FC mov byte PTR [ebp-4], Cl
11: A = P [1];
0040106d 8B 55 EC mov edX, dword ptr [ebp-14h]
00401070 8A 42 01 mov Al, byte PTR [edX + 1]
00401073 88 45 FC mov byte PTR [ebp-4], Al
The first type reads the elements in the string directly into the CL register, while the second type reads the pointer value into EDX. Reading the characters based on edX is obviously slow.
Whether it is a heap or a stack, it is necessary to prevent cross-border phenomena (unless you intentionally cross-border it), because the cross-border result is either a program crash, either it is to destroy the heap and stack structure of the program and generate unexpected results. Even if the above problem does not occur during your program running, you should be careful, maybe it will collapse at some time. Writing stable and secure code is the most important thing.

Static is used to control the storage and visibility of variables.

When a variable defined in a function is executed to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it? The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ).

A Data Object is required to serve the entire class rather than a specific object, and the encapsulation of the class is not damaged. That is, the member is required to be hidden inside the class and invisible to the outside world.

Static internal mechanism:

Static data members must exist at the beginning of the program. Because the function is called during the running of the program, static data members cannot allocate space and initialize it in any function.

In this way, there are three possibilities for its space allocation. One is the header file of the class's external interface, where there is a class declaration; the other is the internal implementation of the class definition, there is a member function definition for the class, and the third is the global data description and definition before the main () function of the application.

Static data members must actually allocate space, so they cannot be defined in the class declaration (only data members can be declared ). The class declaration only declares the "size and specification" of a class and does not actually allocate memory. Therefore, it is wrong to write a definition in the class declaration. It cannot be an external definition of the class declaration in the header file, because it will duplicate the definition in multiple source files that use the class.

Static is introduced to inform the compiler that the variables are stored in the static storage area of the program rather than the stack space. static data members are initialized in sequence according to the sequence in which they appear. Note that when static members are nested, make sure that the nested members have been initialized. The order of cancellation is the reverse order of initialization.

Static advantages:

Memory can be saved because it is public to all objects. Therefore, for multiple objects, static data members only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.

When referencing static data members, use the following format:

<Class name >:: <static member name>

If the access permission of the static data member is allowed (that is, the public Member), you can reference the static data member in the program in the preceding format.

PS:

(1) The static member function of the class belongs to the entire class rather than the Class Object. Therefore, it does not have the this pointer, which leads to the ability to parse static data and static member functions of the class only.

(2) static member functions cannot be defined as virtual functions.

(3) because static members are declared in the class and operated outside of the class, it is somewhat special to perform the address fetch operation on them. The variable address is a pointer to its data type, the function address type is a "nonmember function pointer ".

(4) because the static member function does not have the this pointer, it is almost equivalent to the nonmember function, and the result produces an unexpected benefit: To become a callback function, this allows us to combine C ++ and C-based XWindow systems, and also successfully apply them to thread functions.

(5) static does not increase the space-time overhead of the program. On the contrary, It shortens the access time of the subclass to the static members of the parent class and saves the sub-class memory space.

(6) Add the keyword "static" before <definition or description> to the static data member.

(7) static data members are stored statically, so they must be initialized.

(8) static member Initialization is different from general data member initialization:

Initialization is performed in a class in vitro without static variables, so as to avoid confusion with general static variables or objects. during initialization, the access control letter private and public of the member are not added;

During initialization, the scope operator is used to indicate the class to which it belongs;

So we can get the format of static data member initialization:

<Data type> <class name >:< static data member name >=< value>

(9) to prevent the influence of the parent class, you can define a static variable of the same as the parent class in the subclass to avoid the influence of the parent class. Here, we need to note that static members are shared by the parent class and subclass, but we have repeatedly defined static members. Will this cause errors? No, our compiler uses a wonderful method: name-mangling is used to generate a unique flag.

Supplement: new delete [], basic types of objects do not have destructor (such as int and char), so the delete [] in the array space composed of basic types should all be available, such: int p = new int [10], delete p and delete [] p. however, for arrays of class objects (such as string strArr = new string [10]), only delete []. for a single new object, only delete cannot delete [] to recycle space.

A typical embedded platform dynamic memory management mechanism

(Http://www.elechome.com/Article/ShowArticle.asp? ArticleID = 170)

Currently, most software on the embedded platform is written in C language. In addition to concise code and efficient operation, the flexible memory operation capability is also an important feature of the C language. However, improper memory operations are often one of the root causes of errors. For example, "Memory leakage"-failure to correctly release allocated dynamic memory is a memory error that is very difficult to detect. The continuous memory leakage will make the program performance fall to the end of completely unable to run, and thus affect all other programs with dynamic memory requirements, some relatively simple embedded platforms may even impede the operation of the operating system. Another example is "writing memory out of bounds". An illegal write memory operation is very likely to damage other data being used in this program, in severe cases, it may also affect other running programs or even the entire system. To this end, this article introduces an enhanced and customizable dynamic memory management module (Fense), which is based on the memory allocation function provided by C language; it can record the memory leakage information during software operation, and also has the ability to monitor memory operations. It can detect the vast majority of dynamic memory write errors.

Fense Design Principles

Creates a two-way linked list (struct Head * stHead) to store information about all allocated dynamic memory blocks. Each node in the linked list corresponds to a dynamic memory block. The node contains the memory size, the source file name and row number when the allocation occurs, and the Fense is deleted from st_Head, check the nodes in st_Head to obtain the value checksum of the node that has not been released. Fense inserts each allocated dynamic memory block into the st_Head of the linked list. The node Structure of the linked list is defined as follows:

Struct Head {

Char file;/Name of the source file to which the file belongs */
Unsigned long line;/* the row number of the allocation */
Size_t size;/* memory size allocated */
Int checksum;/* checksum of linked list nodes */
Struct Head prev, next;/* pointer to the front and back nodes of the double-stranded table */
};

/* Global Two-way linked list */
Struct Head * st_Head = NULL;

To detect write out-of-bounds errors, Fense adds a certain size of memory before and after the memory applied by the user as the monitoring area and initializes it to a predetermined value. In this way, when the program has an out-of-bounds write operation, the predetermined value will change and Fense will detect the error.

Fense implementation
Fense provides memory management functions such as Fense_Malloc, Fense_Free, Fense_Realloc, and Fense_Calloc. The functions and calling formats are consistent with those of malloc, free, realloc, and calloc in C. Limited length. Here, we will only briefly describe the implementation process of Fense_Malloc and Fense_Free,

/* Memory Allocation Function */
Void * Fense_Malloc (size_t size, char * file, unsigned long line)
{
// Check the Fense runtime switch. If Fense is disabled, malloc is called.
// Allocate and return
// Check whether there is zero allocation. If yes, 0 is returned after the warning message is displayed (custom options)
// Allocate memory, including the linked list node area and the Front/back monitoring area
// Initialize the linked list node and save the allocated memory information, including the allocated size, file name, and row number.
// Insert this node into the linked list st_Head
// Calculate the checksum for this node region
// Use the preset value to initialize the monitoring area before/after initialization.
// Fill the user memory area with the preset value (custom options)
// Return the starting position of the user's memory area
}
/* Memory release function */
Void Fense_Free (void * uptr, char * file, unsigned long line)
{
// Check the Fense runtime switch. If Fense is disabled, free translation is called and return
// Check the dynamic memory under all Fense Management (custom options)
// Determine whether the current memory block is in the st_Head linked list. If not, a prompt is displayed.
// Exit the alert information (custom options)
// Check whether the current memory block has out-of-bounds operations
// Delete the linked list node of the current memory block from st_Head
// Re-calculate the checksum of the adjacent nodes before and after the current node
// Fill the released memory area with the preset value (custom options)
// Call free to release the current memory block
}

User and Core

386 or more CPUs implement four privileged-level modes (only two of them are used in Windows). The privileged-level 0 (ring0) is reserved for the operating system code and the device driver code, they work in the core state of the system, while privileged 3 (ring3) is used by common user programs and they work in the user State. The code running on the core state of the processor is unrestricted and can freely access any valid address for direct port access. Code running in the user State is subject to a lot of checks by the processor. They can only access the virtual addresses that can access the page in the user State specified in the page table items mapped to their address space, and only the I/O permit Bitmap (I/O permission Bitmap) in the task status segment (TSS) can be) (In this case, the iopl In the processor status and Control Mark register eflags is usually 0, indicating that the lowest privilege level for direct I/O is ring0 ). The above discussion is only limited to operating systems in protection mode. Real-mode operating systems such as DoS do not have these concepts, and all the code can be seen as running in the core state. Since running in the core State has so many advantages, the virus certainly has no reason not to get ring0. The switch from ring3 to ring0 in processor mode occurs when the control is transferred. There are two situations: the long transfer command call of the Access Call door, And the int command of the access interrupt door or trap door. The transfer details involve complex protection checks and stack switching. For more information, see relevant materials. Modern Operating systems usually use the interrupt gate to provide system services, and execute a command to switch the mode. on Intel x86, this command is int, for example, in Win9x, It is int30 (protection mode callback), int80 in Linux, and int2e in winnt/2000. User-mode service programs (such as system DLL) request system services by executing an intxx, and then the processor mode switches to the core state, the corresponding system code working on the core State serves this request and sends the result to the user program.

The user State is also called the object state, and the core State is also called the tube state.
In x86, it can be understood as ring3 and ring 0.
Commands that can be called only in some core states are not permitted due to low user-state permissions.

These are two memory protection modes. Each page in a 4G address space of a process is marked as whether it is in the core State, and all pages in the system address space are in the core state, the page in the user space is in the user State.
The only way to access the page marked as the core is to run in the core State, and only the operating system and the device driver can run in the core state.
Therefore, an application cannot run itself in the core State, which provides a strong level of memory protection for applications and operating systems. Changing user State cannot cause system crash. Of course, applications can load device drivers to enter the core State to modify system data.

In MFC, message transmission is completed in user mode. Most of the synchronization objects are in the core State. Only the CriticalSection can work in the user State (or enter the core State). Therefore, the CriticalSection cannot be used for inter-process synchronization.

For your security, please only open the URL with reliable source

Open website {
Share. safelink. close (); return false;
} "Href =" http://writeblog.csdn.net/# "> cancel

From: http://hi.baidu.com/springwang8020/blog/item/c05f71cfbbeb3c5c0eb3457c.html understanding: learning ......, It is necessary to have a deeper understanding of the computer's memory usage and management. A deeper understanding of the meaning of pointers.

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.