C/C ++ language learning-Memory Allocation Management and Language Learning

Source: Internet
Author: User

C/C ++ language learning-Memory Allocation Management and Language Learning

1The memory occupied by a C-compiled program is divided into the following parts:

1. stack: automatically allocated by the compiler when the program is running, storing the function parameter values and local variable values. The operation method is similar to the stack in the data structure. The compiler Automatically releases the program when it ends.

2. heap-another storage area is opened in the memory. Generally, it is assigned to the programmer for release. If the programmer does not release the program, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.

3. Global (static)-memory allocated upon compilation by the compiler. Global variables and static variables are stored in one area, and initialized global variables and static variables are stored in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The program is released by the system after it ends.

4. Text Constant Area-constant strings are placed here. The program is released by the System

5. program code area-stores the binary code of the function body.

Example Program

1 char aaa [3*2048*2048]; 2 int a = 0; // global initialization zone 3 char * p1; // global uninitialized Zone 4 int main () {5 int B; // Stack 6 char s [] = "abc"; // stack char * p2; // stack 7 char * p3 = "123456 "; // "123456/0" is in the constant zone, and p3 is in the stack. Static int c = 0; // global (static) initialization Zone 8 p1 = (char *) malloc (10); 9 p2 = (char *) malloc (20 ); 10 // The allocated 10-byte and 20-byte areas are in the heap area. 11 strcpy (p1, "123456"); // 123456/0 is placed in the constant area 12}View Code

2And memory allocation methods:

1. Distribution from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.
2. Create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
3. Allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by the programmer and flexible to use. However, if space is allocated on the stack, the programmer has the responsibility to recycle it. Otherwise, the running program may experience memory leakage, frequent allocation and release of heap space of different sizes will generate fragments in the heap.

3Comparison of heap and stack
3.1 Application Method
Stack: automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.
Heap: the programmer needs to apply for the heap and specify the size. In C, the malloc function contains the new operator in C ++.
For example, p1 = (char *) malloc (10); p1 = new char [10];
For example, p2 = (char *) malloc (10); p2 = new char [20];
But note that p1 and p2 are in the stack.
3.2 system response after application
STACK: as long as the remaining space of the stack is greater than the requested space, the system will provide the program with memory. Otherwise, an exception is reported to indicate that the stack overflows.
Heap: First, you should know that the operating system has a linked list that records the idle memory address. When the system receives the application, it will traverse the linked list, find the heap node with the first space greater than the requested space, delete the node from the idle node linked list, and allocate the space of the node to the program.
For most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code can correctly release the memory space.
Because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
3.3 application size limit
STACK: in Windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.
3.4 Comparison of Application Efficiency
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
Heap is the memory allocated by new. It is generally slow and prone to memory fragments, but it is most convenient to use.
In addition, in WINDOWS, the best way is to use VirtualAlloc to allocate memory. Instead of heap or stack, it simply keeps a fast memory in the address space of the process, although it is the most inconvenient to use. However, it is fast and flexible.
Storage content in 3.5 heap and stack
STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call Statement, then there are various parameters of the function. In most C compilers, the parameters are from right to left and then the local variables in the function. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.
3.6 comparison of access efficiency
Char s1 [] = "";
Char * s2 = "B ";
A is assigned a value at runtime, while B is determined at compilation. 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:

1 int main () {2 char a = 1; 3 char c [] = "1234567890"; 4 char * p = "1234567890"; 5 a = c [1]; 6 a = p [1]; 7 return 0; 8}View Code

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 specified needle value into edx and then reads the characters according to edx, which is obviously slow.

Conclusion 3.7
The main differences between stack and stack 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 stacks, there is usually a certain amount of space. For example, under VC6, the default stack space is 1 MB. Of course, this value can be modified.
Fragmentation problem: for the heap, frequent new/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments, reducing program efficiency. For the stack, this problem will not exist, because the stack is first in and then out of the queue, they are so one-to-one correspondence, 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 the stack has been popped up. For details, refer to the data structure.
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.
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. The dynamic allocation is implemented by the malloca function, but the dynamic allocation of stacks is different from that of stacks. The dynamic allocation is released by the compiler without manual implementation.
Allocation Efficiency: the stack is the data structure provided by the machine system. The computer will provide support for the stack at the underlying layer: allocate dedicated registers 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 will search for available sufficient space in heap memory according to 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 the result. Obviously, the heap efficiency is much lower than the stack efficiency.
From this point, we can see that compared with the stack, the use of a large number of new/delete operations may easily cause a large amount of memory fragments; because of 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.
Although the stack has so many advantages, but because it is not so flexible as the heap, sometimes it is better to allocate a large amount of memory space.
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, or destroy the program's heap and stack structure to produce unexpected results.

4. Comparison between new/delete and malloc/free
From the perspective of C ++, using new to allocate heap space can call the class constructor, while the malloc () function is just a function call and does not call the constructor, it accepts an unsignedlong parameter. Similarly, delete calls the Destructor before releasing the heap space, while the free function does not.

1 class Time {2 public: 3 Time (int, string); 4 ~ Time () {5 cout <"call Time's destructor by:" <name <endl; 6} 7 private: 8 int hour; 9 int min; 10 int sec; 11 string name; 12}; 13 Time: Time (int h, int m, int s, string n) {14 hour = h; 15 min = m; 16 sec = s; 17 name = n; 18 cout <"call Time's constructor by:" <name <endl; 19} 20 int main () {21 Time * t1; 22 t1 = (Time *) malloc (sizeof (Time); 23 free (t1); 24 Time * t2; 25 t2 = new Time (0, 0, 0, "t2"); 26 delete t2; 27}View Code

Attributes of a variable: data type, storage type, scope, and storage period.

1. Data Type: we are familiar with int, char, long, etc. There is nothing to say;

2. Storage types: auto, static, register, and extern;

3. Scope: the region where the variable can be used in the program;

4. Storage Period: The storage period of the variable in the memory.

Storage Type: it is described in the following scope description.

Scope: local variables and global variables

Local variables:

Automatic variables (dynamic local variables, defined in the function, without static declaration of static local variables, the value disappears after leaving the function)

Static local variables: (defined in the function, the static local variables are declared with static, and the value is retained when the function is left)

Register variables: (frequently used variables are stored in the cpu registers. It is not necessary in C ++, because it is like a built-in function. How can we handle the computation of the compiler and leave the function, value disappears) form parameter (when passing a parameter, it is more often defined as an automatic variable or a register variable)

Global variables:

External variables (variables defined outside the external function. Other files can be declared using extern and disappear when the program ends)

Static external variables (other files are not available and disappear when the program ends)

Storage Period: dynamic and static storage

Dynamic Storage:

Automatic variable (effective in this function)

Register variable (valid in this function)

Parameter (valid for this function)

Static storage:

Static local variables (valid for this function)

Static global variables (valid in this file)

External variables (other files can be referenced)

In addition, there are constant storage, dynamic application space (new, malloc), and so on, which have been described in detail above.


When does C language require dynamic memory allocation?

I will explain the function of dynamically allocating memory in C language. Some beginners may have to ask: why do we need to use functions to dynamically allocate memory? Isn't the system automatically allocate memory ??
Since someone may ask such a question, I 'd like to explain it here!
First, let's get familiar with the computer memory! There are four memory areas in the computer system: 1) STACK: store some defined local variables and parameters (formal parameters) in the stack; 2) character constant areas: it mainly stores some character constants, such as char * p_str = "cgat", where "cgat" is stored in the character constant area; 3) Global Area: store some global and static variables in the global area. 4) Heap: the heap mainly uses the dynamically allocated storage space, that is, the dynamically allocated memory space we will talk about next.
When do we need to dynamically allocate memory space? For example. Int * p; we have defined a pointer to the int type p; p is used to store the value of an address.
This variable allocates space to give it a clear point, for example! You have now made a roadmap pointing to the direction, but you have not set this roadmap to a specific position. That is to say, the current roadmap is blind, in this way, we cannot use it to identify where the East is, where the West is, what the North is, and what the South is. Although we have defined a pointer variable in the computer's memory, we have not asked this variable to indicate the address of an exact int type variable, therefore, we must give it a clear direction. In this way, we need to define its direction by dynamically allocating memory!
We have encountered this situation when we first touched the pointer. int * p; p = & a; this method is not the dynamic memory allocation of the pointer. This is called the initialization of the pointer variable! Initialization also allows the pointer variable to be directed.
Int * p; p = malloc (n * sizeof (type name); we allocated an address for a pointer Variable p through the malloc () function, in this way, the value we typed on the keyboard is stored in p, and then we can perform specific operations on this p, such as scanf ("% s", p) and so on. When we end p operations, we need to release p's memory space. Why do we need to release the memory? As I have mentioned above, the dynamically allocated variables are stored in the heap, but the heap space is not infinitely large, maybe we may not be able to find anything when compiling a small program, but for those large programs, if we release the heap space in time, memory leakage will occur. The so-called memory leakage is because the heap space is dynamically allocated to the north, so when we use the dynamic heap space allocation, there is not enough space for us to use, in this way, we need to occupy the original space, that is, we will store other space for the value we typed. This will cause the original stored data to be destroyed, resulting in Memory leakage.
At the same time, when using the malloc () function, we should also note that when we release the space, we should also assign the original pointer variable to a NULL, that is, assign a NULL pointer, use it next time! What if we do not grant | NULL rows ?? The answer is: no! If we do not assign a null pointer, the original pointer variable becomes a wild pointer! What is a wild pointer? A wild pointer is a pointer that is not explicitly pointed to, and the system does not know where it will point. It is very dangerous to use a wild pointer. Therefore, when we use malloc () you must assign a null pointer to all functions! Compared with the malloc () function, the calloc () function does not need to be NULL. This is because () the system will automatically assign the original pointer to a null pointer, that is, "0 ". The prototype of the calloc () function is void * calloc (count, sizeof (type name); for example, p = (char *) calloc (4, sizeof (char )); we allocated a space of "4" to the char pointer for p.
In addition to malloc () and calloc (), there is also a function for dynamically allocating space realloc ...... the remaining full text>

Memory Allocation in C Language

Take vc6 as an example. In debug mode, the stack grows down.
Therefore, the arr [size] is variable a. If you do not believe it, you can print the address of the variable a/B and the value of the array name arr.
Of course, this is in debug mode. If it is in release mode, the result is not necessarily. The result is often a running error, or a compiler or an operating platform, the results are different.
In short, cross-border access is not advocate, although I admit that sometimes this C feature can be used to simplify the problem, for example, I will often make multidimensional arrays one-dimensional, however, the C language only does not require array out-of-bounds checks. In fact, many C compilers can perform out-of-bounds checks by default. I remember vs08 or vs. By default, cross-border checks can be performed.

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.