Heap: The operating system has a linked list that records the free memory address, when the system receives the application, it iterates through the list, finds the first heap node that is larger than the requested space, and then removes the node from the list of idle nodes and assigns the node's space to the program, in addition, for most systems, The size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code can properly free up the memory space. We often say that the memory leak, the most common is the heap leakage (and resource leakage), it refers to the program is leaking in the run, if the program is shut down, the operating system will help release the leaked memory. Stack: The address of the next instruction in the main function of the first stack in the function call (the next executable statement of the function call statement) is then the arguments of the function, and in most C compilers, the arguments are moved from right to left and then local variables in the function. (Reference textbook)
1#include <iostream>2 3 intMainvoid)4 {5 using namespacestd;6 Double* A =New Double(5);7 intb =1;8cout << *a <<Endl;9cout << a <<Endl;Tencout << &b <<Endl; One A delete A; - return 0; - } the - /*********************************** - * 5 - * 0X1002C20----->heap (heap) + * 0X7FFE7BD82EC4----->stack (Stack) - * *********************************/ +~
Heap and Stack address format are not the same. Indicates that they are not in the same place in memory.
The memory used by a program compiled by C + + is divided into the following sections
1
2 generally released by programmers, os< Span style= "FONT-SIZE:12PT; line-height:115%; Font-family: the song body; > recovery
3 -
4, literal constant area - The constant string is put here. released by the system after the program is finished
5. Program code Area -binary code that holds the function body. (copy from: http://www.cnblogs.com/likwo/archive/2010/12/20/1911026.html)
Char a[] = "I like comouter!"; I like Conputer when run-time is assigned. means that each time the program runs the address that stores the string is different.
Char *b = "I like computer!" I like Conouter is allocated at compile time. means that once the program generates executable code, the address of each compilation of the string no longer changes.
1#include <iostream>2 3 intMainvoid)4 {5 using namespacestd;6 7 CharA[] ="I like comouter!";8 Char*b ="I like conputer!";9 Tencout << (void*) a <<Endl; Onecout << (void*) b <<Endl; A - return 0; - } the - /************************************* - * [[email protected] cc] #c + + s./string - * 0x7ffe4868c8e0 + * 0x400991 - * [[email protected] cc] #c + + s./string + * 0X7FFDD6C640E0 A * 0x400991 at * [[email protected] cc]#./string - * 0x7fffaffc7ce0 - * 0x400991 - * [[email protected] cc]# - * ***********************************/
The following is a copy of other people's blog, the blog address above has been given:
Comparison of 2.6 access efficiency
Char s1[] = "AAAAAAAAAAAAAAA";
Char *s2 = "BBBBBBBBBBBBBBBBB";
AAAAAAAAAAA is assigned at run time;
and bbbbbbbbbbb is determined at compile time;
However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to ( for example, a heap ) .
Like what:
#I nclude
void Main ()
{
char a = 1;
Char c[] = "1234567890";
Char *p = "1234567890";
A = c[1];
A = p[1];
Return
}
The corresponding assembly code
10:a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
0040106A 4D FC mov byte ptr [ebp-4],cl
11:a = p[1];
0040106D 8B-EC mov edx,dword ptr [ebp-14h]
00401070 8A mov al,byte ptr [edx+1]
00401073 FC mov byte ptr [ebp-4],al
The first reads the elements in the string directly into the register cl, while the second one reads the pointer values into edx, which is obviously slow to read the characters according to EdX .