In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The memory consumed by a program compiled by Candlestick + is divided into the following parts
1, stack area (stack)-automatically allocated and released by the compiler, storing the parameter values of the function, the values of local variables, etc. Its
The mode of operation is similar to the stack in the data structure.
2. Heap-usually allocated and released by the programmer. If the programmer does not release, the program may be returned by OS at the end of the program.
Take it. Note that it is different from the heap in the data structure, and the way of allocation is similar to the linked list, hehe.
3. Global area (static zone) (static)-global and static variables are stored together and initialized
Global variables and static variables are in the same area, and uninitialized global variables and uninitialized static variables are adjacent to each other.
An area. -released by the system at the end of the program.
4. The text constant area-the constant string is put here. Released by the system at the end of the program
5. Program code area-the binary code that stores the body of the function.
2. Example program
It was written by a senior, and it's 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 Universe 0 is in the constant area and p3 is on the stack.
Static int c = 0; global (static) initialization zone
P1 = (char *) malloc (10)
P2 = (char *) malloc (20)
The area with 10 and 20 bytes allocated is in the heap area.
Strcpy (p1, "123456"); 123456 Universe 0 is placed in the constant area, and the compiler may associate it with the "123456" pointed to by p3.
Optimize it into one place.
}
Second, the theoretical knowledge of stack 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
Between
Heap:
The programmer needs to apply for it and specify the size, and the malloc function in c
Such as p1 = (char *) malloc (10)
Using the new operator in C++
For example, p2 = new char [10]
But note that p1 and p2 themselves are in the stack.
2.2
Response of the system after application
Stack: as long as the remaining space of the stack is larger than the applied space, the system will provide memory for the program, otherwise it will report an exception that the stack will overflow
Out.
Heap: first of all, you should know that the operating system has a linked list of free memory addresses, when the system receives an application from the program
The linked list is traversed to find the first heap node whose space is larger than the applied space, and then the node is linked from the free node list
And allocate the space of the node to the program, and for most systems, it will be in this memory space
Record the size of this allocation at the first address so that the delete statement in the code can release the memory space correctly.
In addition, since the size of the heap node found is not necessarily equal to the size of the application, the system will automatically change the excess one
Put the points back into the free linked list.
2.3 limit on the size of the application
Stack: under Windows, a stack is a data structure extended to a low address, a contiguous area of memory. The meaning of this sentence
The address at the top of the stack and the maximum capacity of the stack are predetermined by the system. Under WINDOWS, the size of the stack is 2m (also have
It is said to be 1m, which is a constant determined at compile time. If the requested space exceeds the remaining space in the stack, the
Prompt overflow. Therefore, there is less space that can be obtained from the stack.
Heap: a heap is a data structure that extends to a high address and is a discontiguous area of memory. This is because the system uses linked lists to store
The free memory address of is naturally discontiguous, while the traversal direction of the linked list is from low address to high address. Heap size
Limited by the effective virtual memory in the computer system. Thus it can be seen that the space obtained by the heap is more flexible and larger.
In fact, this is the case in the computer, the program grows from low address to high address, and the stack grows from high address to low address. When they meet in the middle, the logical space of the computer is exhausted.
2.4 comparison of application efficiency:
The stack is automatically allocated by the system, and the speed is fast. But programmers can't control it.
Heap is memory allocated by new, which is generally slow and prone to memory fragmentation, but it is the most convenient to use.
In addition, under WINDOWS, the best way is to allocate memory with VirtualAlloc, which is not on the heap or on the stack
Keep a piece of memory directly in the address space of the process, although it is the most inconvenient to use. But the speed is fast and the most flexible.
2.5 Storage in heaps and stacks
Stack: when a function is called, the first instruction in the stack is the next instruction in the main function (the next instruction in the function call statement can
The address of the execution statement, followed by the parameters of the function, which in most C compilers enter the stack from right to left
, followed by local variables in the function. Note that static variables are not on the stack.
When this function call ends, the local variable goes off the stack first, then the parameters, and finally the pointer at the top of the stack points to the place where it was first stored.
Address, that is, the next instruction in the main function, from which the program continues to run.
Heap: the size of the heap is usually stored in a byte at the head of the heap. The details in the heap are arranged by the programmer.
2.6 comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaaaa"
Char * S2 = "bbbbbbbbbbbbbbbbb"
Aaaaaaaaaaa is assigned at run time
And bbbbbbbbbbb is determined at compile time.
However, in later access, the array on the stack is faster than the string (such as the heap) pointed to by the pointer.
For example:
# include
Void main ()
{
Char a = 1
Char c [] = "1234567890"
Char * p = "1234567890"
A = c [1]
A = p [1]
Return
}
Corresponding assembly code
A = c [1]
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4], cl
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 reads the elements in the string directly into the register cl, while the second reads the pointer value to
In edx, it is obviously slow to read characters according to edx.
2.7 Summary:
The difference between a heap and a stack can be seen in the following metaphor:
Using stacks is like going to a restaurant to eat, just order (apply for), pay, and eat (use) when you are full.
Go, do not pay attention to cutting vegetables, washing vegetables and other preparation work and washing dishes, washing pans and other clean-up work, his advantage is fast, but since
The degree of freedom is small.
Using heap is like making your own favorite dishes, which is troublesome, but it is more in line with your own taste and is free.
The degree is great.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.