I read a post about C language memory allocation written by someone tonight and found that I really want to find it, so I almost got my favorite...
Let's take a look at two sections of code:
Char * toStr () {char * s = "abcdefghijkl"; return s;} int main () {cout <toStr () <endl; return 0 ;} char * toStr () {char * s = "abcdefghijkl"; return s;} int main () {cout <toStr () <endl; return 0 ;} and [cpp] view plaincopyprint? Char * toStr () {char s [] = "abcdefghijkl"; return s;} int main () {cout <toStr () <endl; return 0 ;} char * toStr () {char s [] = "abcdefghijkl"; return s;} int main () {cout <toStr () <endl; return 0 ;}
The previous code is printed as a string, and the subsequent code is garbled. I remember when I learned the C language, I mentioned that strings are processed as character arrays. Therefore, the character array name is equivalent to a pointer to the first address. So
1. char * s = "abcdefghijkl ";
2. char s [] = "abcdefghijkl ";
The two expressions seem to be the same, but why are the program results different? The reason is that you are not familiar with memory allocation. Of course, the current C language teaching materials will not talk about.
Explanation:
The meaning of the program is relatively simple and does not need to be explained.
In the first expression, the pointer s is a local variable and its scope is within the toStr function. It returns the address it points to. s is destroyed after the return, and the address s points to is returned. The final print is correct.
In the second expression, we will ask what is the difference between the second expression and the first expression. Why is it wrong? The reason is that although the first pointer s is a local variable, it is allocated in the stack space and the scope is inside the function, but the content it points to "abcdefghijkl" is a constant, it is allocated in the constant area of the program. It is not destroyed until the whole program ends. Second, s is an array allocated to the stack space. "abcdefghijkl" is put into the array as each element of the array. Once the function exits, the memory in the stack is released. Although an address is returned, its meaning is lost.
The above example shows how to learn about memory allocation.
First, you need to understand that the variable type and its storage class are two concepts.
There is no direct relationship between data types and memory management.
1. Memory occupied by a C/C ++ compiled program is divided into the following parts:
1. stack: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.
2. heap-generally assigned and 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. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.
3. Global (static), storage of global and static variables is placed in one area, and initialized global and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program is released by the system.
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.
Ii. Example Program
This is written by a senior. It is very detailed.
// Main. cpp int a = 0; // global initialization zone char * p1; // global uninitialized Zone main () {int B; // stack char s [] = "abc "; // stack char * p2; // stack char * p3 = "123456"; // 123456 \ 0 is in the constant zone, and p3 is in the stack. Static int c = 0; // global (static) initialization zone p1 = (char *) malloc (10); p2 = (char *) malloc (20 ); // The allocated 10-byte and 20-byte areas are in the heap area. Strcpy (p1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it to a place that points to "123456" of p3. } // Main. cpp int a = 0; // global initialization zone char * p1; // global uninitialized Zone main () {int B; // stack char s [] = "abc "; // stack char * p2; // stack char * p3 = "123456"; // 123456 \ 0 is in the constant zone, and p3 is in the stack. Static int c = 0; // global (static) initialization zone p1 = (char *) malloc (10); p2 = (char *) malloc (20 ); // The allocated 10-byte and 20-byte areas are in the heap area. Strcpy (p1, "123456"); // 123456 \ 0 is placed in the constant area, and the compiler may optimize it to a place that points to "123456" of p3. }
This gives you a better understanding of program memory allocation.
In fact, it includes other programming languages, Java, and so on. They all have so-called stack space, heap space, and constant zone. After writing the program, we often find inexplicable errors, or the memory is slowly swallowed up, this is why.
The following are the theoretical knowledge of heap and stack:
2.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: requires the programmer to apply for and specify the size. In c, the malloc Function
For example, p1 = (char *) malloc (10 );
Use the new operator in C ++
For example, p2 = (char *) malloc (10 );
But note that p1 and p2 are in the stack.
2.2 system response after application
STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.
Heap: First, you should know that the operating system has a linked list that records idle memory addresses. When the system receives a program application,
The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated 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. In addition, 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.
2.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.
Therefore, the array of automatic variables (inside the function) in the program cannot be very large, because the size of the stack (this is the stack segment of the program we usually say, and the large array has a segment overflow) is limited, you can apply for a global variable because it is allocated in the static zone and the size is not limited.
2.4 comparison of application efficiency:
The stack is automatically allocated by the system, which is faster. But programmers cannot control it.
The heap is the memory allocated by new, which is generally slow and prone to memory fragments. However, 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 is to reserve 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 2.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), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by 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.
2.6 comparison of access efficiency
Char s1 [] = "aaaaaaaaaaaaa ";
Char * s2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime;
Bbbbbbbbbbbbb is determined during 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:
# Include <stdio. h>
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.