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)05/31 Report--
This article Xiaobian for you to introduce in detail "C language stack, heap and static storage how to use", the content is detailed, the steps are clear, and the details are handled properly. I hope this "C language stack, heap and static storage how to use" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
I. Stack in the program
Stack is one of the most important concepts in modern computer programs.
The stack is used to maintain the function call context in the program
The parameters and local variables in the function are stored on the stack
The stack holds the maintenance information needed for a function call
Parameters.
Return address
Local variable
Calling context
Second, the calling process of the function
Each function call corresponds to an activity record on the stack
The activity record of the calling function is located in the middle of the stack
The activity record of the tuned function is at the top of the stack.
Third, stack changes of function calls
Start running from main ()
Main () calls f ()
When main () is returned from the f () call
4. Data on the function call stack
When a function is called, the corresponding stack space is dedicated before the function returns.
After the function call ends, the stack space is freed and the data is no longer valid
Let's look at a pointer to stack data:
# include int* g () {int a [10] = {0}; return a;} void f () {int I = 0; int b [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int* pointer = g (); for (I = 0; I < 10; iBI +) {b [I] = pointer [I];} for (I = 0; I < 10) ) {printf ("% d\ n", b [I]);}} int main () {f (); return 0;}
The output is as follows:
If you put
For (I = 0; I < 10; iTunes +)
{
B [I] = pointer [I]
}
After comments, print the data in pointer [I] directly, as follows:
# include int* g () {int a [10] = {0}; return a;} void f () {int I = 0; int b [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int* pointer = g (); / * for (I = 0; I < 10; iBI +) {b [I] = pointer [I];} * / for (I = 0; I < 10) Int main +) {printf ("% d\ n", pointer [I]);}} int main () {f (); return 0;}
The output is as follows:
Why do you print the value in pointer [I] like this? Because the space pointer points to is the stack space, the stack space is released after the g () function returns. The printf function is called after being released, and the printf function needs to create an activity record on the stack. This activity record will have the parameter information and return value of the printf function, so the data in memory pointed to by the pointer has been changed due to the call to the printf function. Therefore, you cannot return the address of a local variable or the array name of a local array.
5. Heap in the program
A heap is a reserved memory space in a program, which can be used freely by the program.
The memory used by the program in the heap will remain valid until it is actively released.
Why do you need a stack when you have a stack?
A: the data on the stack is released after the function returns and cannot be passed outside the function, such as a local array.
The heap space is obtained by calling library functions in C language programs.
Header file: malloc.h
Malloc-dynamically request heap space in bytes
Free-return heap space to the system
The way the system manages the heap space
Free chain table method, bitmap method, object pool method, etc.
Take int* p = (int*) malloc (sizeof (int)); for example, to apply for a 4-byte size, it is found that it is closest to the node of 5 Bytes after traversing. After finding a usable cell, the address of the cell is returned to the p pointer. As mentioned before, when using malloc to apply for memory space, the memory space returned may be slightly larger than the actual memory space applied for. The reason is that in systems such as free linked list management heap space, it will find the nearest one, and the found memory space is generally greater than or equal to the required memory space. If all the free memory units under the 5 Bytes node are used up, it will find the memory unit under the 12 Bytes node. In this way, it is possible that the memory space returned by malloc is larger than the memory space actually requested by yourself.
6. Static storage area in the program
Static storage allocates space as the program runs
The life cycle of the static store until the end of the program
The size of the static storage area is determined at compile time of the program.
Static storage is mainly used to store global variables and static local variables.
The information of the static storage area will eventually be saved to the executable program
Let's take a look at the validation code for a static store:
# include int gadolv = 1; static int g_vs = 2; void f () {static int g_vl = 3; printf ("% p\ n", & g_vl);} int main () {printf ("% p\ n", & g_vs); printf ("% p\ n", & g_vs); f (); return 0;}
The output is as follows:
You can see that the three addresses are stored sequentially because all three variables are stored in the static storage area of the program, which has a fixed starting address in the program.
After reading this, the article "how to use the C language stack, heap and static storage area" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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.