In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to call stacks and functions in C++". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn the article "how to call stacks and functions in C++".
One C++ program memory allocation
1) create it on the stack. When the function is executed, the storage units of local variables in the function are created on the stack, and these storage units are automatically released at the end of the function execution. The stack memory allocation operation is built into the instruction set of the processor and is generally accessed by registers, which is very efficient, but the allocated memory capacity is limited.
2) allocation from the heap, also known as dynamic memory allocation. The program uses malloc or new to request any amount of memory while it is running, and the programmer is responsible for when to use free or delete to free memory. The lifetime of dynamic memory is determined by the programmer and is very flexible to use.
3) allocate from static storage area. The memory program has been allocated when it is compiled, and this memory block exists throughout the run of the program. For example, global variable, static variable.
4) the text constant is allocated in the text constant area and is released by the system at the end of the program.
5) Program code area.
Classic example: (the code comes from a network master, but the original author is not found)
Code
# include
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Comparison of two and three kinds of memory objects
The advantage of stack object is that it is automatically generated and destroyed at the right time, so there is no need for programmers to worry about it. And stack object creation speed is generally faster than heap object, because when allocating heap object, operator new operation will be called, operator new will use some kind of memory space search algorithm, and the search process may be very time-consuming, it is less troublesome to generate stack object, it only needs to move the top pointer of the stack. However, it should be noted that the stack space is usually small, usually 1MB~2MB, so larger objects are not suitable for allocation in the stack. In particular, it is best not to use stack objects in recursive functions, because as the depth of recursive calls increases, the required stack space increases linearly, and when there is not enough stack space, it will lead to stack overflows, resulting in run-time errors.
The programmer is responsible for the creation and destruction of heap objects, so memory problems can occur if they are not handled properly. If you allocate a heap object, but forget to release it, you will have a memory leak; and if you have released the object, but do not set the corresponding pointer to NULL, the pointer is the so-called "hanging pointer". When you use this pointer again, there will be illegal access, which will cause the program to crash in serious cases. But efficient use of heap objects can also greatly improve code quality. For example, if we need to create a large object and need to be accessed by multiple functions, it is undoubtedly a good choice to create a heap object at this time, because we can share the object by passing the pointer of the heap object between the functions, which greatly reduces the copy time of the object compared with the transfer of the whole object. In addition, the capacity of the stack is much larger than the stack space. In fact, when there is not enough physical memory, if a new heap object needs to be generated at this time, there is usually no run-time error, but the system uses virtual memory to extend the actual physical memory.
Static storage area. All static and global objects are allocated in the static storage area. Global objects are allocated before the main () function is executed. In fact, before the display code in the main () function executes, a compiler-generated _ main () function is called, and the _ main () function does the construction and initialization of all global objects. Before the end of the main () function, the compiler-generated exit function is called to release all global objects. For example, the following code:
Void main (void)
{
... ... / / explicit code
}
In fact, it was transformed into this:
Void main (void)
{
_ main (); / / implicit code, generated by the compiler, to construct all global objects
... ... / / explicit code
... ...
Exit (); / / implicit code, generated by the compiler, to release all global objects
}
In addition to global static objects, there are local static objects and static members of class. Local static objects are defined in functions, just like stack objects, except that they are preceded by a static keyword. The lifetime of a local static object is the first time its function is called, or rather, when the static object's declaration code is executed for the first time, the static local object is generated and the object is not destroyed until the end of the entire program. The life cycle of the static member of the class is the first call of the class to the end of the program.
Three function calls and stack
1) the compiler generally uses the stack to store function parameters, local variables, etc., to achieve function calls. Sometimes functions have nested calls, and there is information about multiple functions in the stack, each of which occupies a contiguous area. The area occupied by a function is called a frame. At the same time, the stack is thread independent, and each thread has its own stack. For example, the following simple function call:
In addition, the cleaning method of the function stack determines that when the function call ends, the calling function or the called function cleans up the function frame. There are two ways to clean the function stack in VC:
Parameter passing order who is responsible for cleaning up the stack occupied by parameters _ _ stdcall is tuned from right to left function _ _ cdecl caller from right to left
2) with the above knowledge as the groundwork, let's take a closer look at the changes in the stack when a function is called:
The code is as follows:
Code
Int Add (int x, int y)
{
Return x + y
}
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
Undefined
For the above code, we are divided into four steps, of course we only draw the impact of our code on the stack, the rest of us assume that they do not exist, !
First, int * pi = new int (10); int * pj = new int (20); int result = 0; the stack changes are as follows:
Second, Add (* pi,*pj); the stack is as follows:
Third, give the result of Add to result. The stack is as follows:
Fourth, the delete pi; delete pj; stack is as follows:
Fifth, when main () exits, the stack is as follows, which is equivalent to before main execution, !
These are all the contents of the article "how to call stacks and functions in C++". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.