table of Contents
The space owned by a process.
From high address to low address:
Variable type
Local constants, global constants, global variables, local variables, local static variables, global static variables, local temporary variables, global temporary variables.
1. Stack
Temporary variables, function parameters, local constants.
2. Heap
Dynamically allocated variables. (Allocated by new or malloc.)
The efficiency of the heap is slower than the stack.
3. Static storage area
Global static variables, local static variables (so they will not disappear with the completion of the function), global variables.
4. Constant area
Global constant.
5. Code area
Store the code.
//main.cpp
int a = 0; //全局初始化区
char *p1; //全局未初始化区
int main()
{
int b; //栈区
char s[] = "abc"; //栈区
char *p2; //栈区
char *p3 = "123456"; //123456在常量区,指针p3在栈上。
static int c =0; //全局(静态)初始化区
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
//分配得来得10和20字节的区域就在堆区。
strcpy(p1, "123456");
//123456放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
}
What resources does a thread own when it is multi-threaded?
PC program counter, register, stack, status word.
If some resources are not exclusive and will cause thread operation errors, then the resource is exclusively shared by each thread, and other resources are shared by all threads in the process.
In other words, in general, a thread cannot access the data on the stack of another thread.