The five major memory partitions are divided into 5 districts in C , which are piles, stacks, free storage, global / static storage and constant storage. Stack is the storage area that is automatically clear when it is assigned by the compiler when needed, and automatically clear the storage area of the variable when not needed. The variables inside are usually local variables, function parameters, and the like. Heap is those memory blocks allocated by new, their release compiler does not depends, and is controlled by our application. Generally, a New is to correspond to a delete. If the programmer is not released, the operating system will automatically reclaim after the program is over. The free storage area is those that are allocated by Malloc, and he and the heap are very similar, but it uses free to end your own life. The global / static storage area, global variables and static variables are assigned to the same memory. In the previous C language, the global variable is divided into initialization and unexpected, and there is no difference in C , they share the same A memory area. Constant storage area, this is a relatively special storage area, which is stored in constants, not allowed to modify (of course, you have to modify the non-normal means, and there are many ways) clearly divide the stacks and stacks on BBS, pile The problem of being in the stack seems to be an eternal topic. It can be seen that beginners are often confused, so I decided to take him the first open knife. First, we will give an example: void f () {int * p = new int [5];} This short sentence contains the stack and stack, see New, we should first think of it, we assigned A pile of memory, then pointer P? He assigned a stack memory, so this sentence means that a pointer P is stored in a stack of memory in the stack memory. In the program, you will determine the size of the memory in the stack, then call the Operator New allocated memory, then return the first address of this memory, put in the stack, his assembly code under VC6 is as follows: 00401028 Push 14H 0040102A Call Operator New (00401060) 0040102032 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 memory So how do you release it? Is DELETE P? Australia, wrong, it should be delete [] P, this is to tell the compiler: I deleted an array, VC6 will work according to the corresponding cookie information to release the memory.
Ok, we go back to our theme: What is the difference between the stack and stack? The main difference is different from the following points: 1. Different management methods; 2, the space size is different; 3, can generate fragmentation; 4, the growth direction is different; 5, the allocation mode is different; management method: In the stack, it is automatically managed by the compiler. No need to control it; for the heap, the release work is controlled by programmers, which is easy to generate Memory Leak. Space size: Under the 32-bit system, the heap memory can reach 4G space, from this perspective, the stack memory is almost no restrictions. However, for the stack, there is generally a certain space size, for example, under VC6, the default stack space size is 1M (like, not clear). Of course, we can modify: Open the project, follow the following: Project-> setting-> link, select Output in Category, then set the maximum value of the stack and commit in RESERVE. Note: The RESERVE minimum is 4Byte; commit is reserved inside the page file of the virtual memory, which makes the stack to open a larger value, which may increase the overhead and startup time of the memory. Debris Problem: For heaps, frequent New / Delete will inevitably cause discontinuous memory space, resulting in a large amount of debris, making program efficiency decrease. For the stack, there will be no such problem, because the stack is the first queue, they are such a one, so that there is no possibility of popping up from the middle of the stack, before he pops up, He has already popped up, detailed can refer to the data structure, here we will not discuss one by one. Growth direction: For heaps, the growth direction is up, that is, the direction increasing to the memory address; for the stack, its growth direction is downward, which is to grow toward the direction of memory addresses. Assignment: Heap is dynamically allocated, no static allocation stacks. There are two types of allocation: static allocation and dynamic allocation. Static allocation is the completion of the compiler, such as partial variables. Dynamic assignments are allocated by the alloca function, but the dynamic allocation and stack of the stack is different. His dynamic allocation is released by the compiler, no need to implement it. Assigning Efficiency: The stack is the data structure provided by the machine system. The computer provides support in the underlying pair of stacks: allocated the address of the stack, the stack out of the stack has a special instruction, which determines the efficiency of the stack. . The stack is provided by the C / C function library. Its mechanism is very complicated, for example, in order to assign a single memory, the library function will search in the stack memory in the stack memory in accordance with certain algorithms (specific algorithms). Sufficient size space, if there is no size enough space (may be too much memory fragmentation), there is a possibility to call the system function to add the memory space of the program data segment, so there is a chance to share the size of the memory, then return. Obviously, the efficiency of the heap is much lower than the stack.
From here we can see, due to the use of a large number of new / delete, it is easy to cause a large amount of memory fragments; due to the unmanaged system support, the efficiency is very low; due to possible switching, The application of memory is more expensive. So the stack is the most widely used in the program. Even if the call is called, the parameters, return addresses, EBP, and local variables are stored in the function call. So, we recommend everyone to use the stack, not the heap. Although there are so many benefits, it is not so flexible compared with the heap, sometimes allocates a lot of memory space, or uses a stack of stacks. Whether it is a stack or stack, it is necessary to prevent the occurrence of cross-bounds (unless you deliberately make it off), because the results of the offshore are either the procedure crash, or destroy the plot, stack structure, resulting in the result, even During your program, there is no problem, you still have to be careful, maybe when you can't collapse, then Debug is quite difficult :) Right, there is one thing, if someone puts up the stack Said, then it means that the stack is not a pile, huh, huh, clear? Static is used to control variables and variables defined inside the visibility function. When the program executes it to its definition, the compiler allocates space on the stack, and the function assigned on the stack is assigned at this function. It will be released, which has a problem: how do you implement if you want to save the value of this variable to the next call? The most easy way to define a global variable, but there are many shortcomings defined as a global variable, the most obvious disadvantage is to destroy the access range of this variable (so that variables defined in this function are not only controlled by this function ). A data object is required for the entire class rather than an object service.
STATIC's internal mechanism: The static data member must exist when the program is running. Since the function is called in the program run, the static data member cannot allocate space and initialization within any function. In this way, its spatial distribution has three possible places, one is the header file as an external interface, there is a class declaration; the second is the internal implementation of the class definition, there is a class of member function definitions; three is the application of the application () Global Data Declaration and Definition before the function. Static data members must actually allocate space, so they cannot be defined in the category declaration (only data members can be declared). Class declarations only declare a class of "size and specifications", do not perform actual memory allocation, so write a definition in class declaration is wrong. It cannot also be defined in the external definition of category in the header file, because it will be repeatedly defined in multiple source files that use this class. Static is introduced to inform the compiler, store the variable in the static storage area of the program, and the static data member is initialized in order, pay attention to the static member nested, and to ensure that the nested members have been Initialized. The order of elimination is the reverse order of initialization.
Static's advantage: save memory, because it is public, so for multiple objects, static data members are only stored for all objects. The value of the static data member is the same, but its value is updated. As long as the value of the static data member is updated once, all objects have been updated after the update, which can improve time efficiency. When references a static data member, use the following format:
PS: (1) The static member function of the class is an object that belongs to the entire class, so it doesn't have this pointer, which results in only the static data and static member functions of the class. (2) Can't define the static member function as a virtual function. (3) Since the static member is declared in the class, it is operated outside, so however, how many special, the variable address is a pointer to its data type, the function address type is a "Nonmember Function Pointer".
(4) Since the static member function does not have the THIS pointer, it is almost equivalent to the nonmember function. The result has an unexpected benefits: becoming a callback function, making us a C and C-Based X Window system combine, and also Successfully applied to the thread function. (5) Static does not increase the time and space overhead of the program. In contrast, she also shortens the access time of the subclass to the statistical member of the parent class, saves the memory space of the subclass. (6) Static data member is adding keyword static in