1. Allocating a variable with heaps and stacks may produce unexpected results in subsequent executions, and the result is that the memory is illegally accessed, causing the contents of the memory to be modified.
The two basic concepts for understanding this phenomenon are that the threads in a process share the heap, while threads in the process maintain their own stacks.
If you declare a member variable, such as Char name[200], the address of name on the stack is freed as the code call ends. In the case of char* name = new CHAR[200], the situation is completely different, unless you explicitly call Delete, the address of the no-name point is not released.
In program B, if you allocate the declaration V and the heap area using the mechanism of the staging variable, the result is different. If you use the stack area, if the variable address is am1-am2 so large, exit B calls when the address will be freed, C function may overwrite this memory, so when program D executes, the content read from memory am1-am2 is changed. If you allocate with new (heap), that is not the case, because the delete is not explicitly called and the heap is shared with the thread, that is, thread 2 can see what thread 1 allocates in the heap, so no rewriting occurs.
2. Thread-shared environments include: Process code snippets, public data for processes (with these shared data, threads can easily communicate with each other), file descriptors opened by processes, processor of signals, current directory of processes and process user ID and process group ID.
Process has these commonalities, but also has its own personality. With these personalities, threads can achieve concurrency. These personalities include:
A. Thread ID
Each thread has its own thread ID, which is unique in this process. The process uses this to identify the thread.
B. The stack of threads
The stack is necessary to ensure that the thread runs independently. A thread function can call a function, and the called function can be nested in layers, so the thread must have its own stack of functions so that the function call can execute normally, not affected by other threads.
C. Error return code
Different threads should have their own error return code variable.
D. Signal masking for threads
Because each thread is interested in a different signal, the thread's signal masking should be managed by the thread itself. But all the threads share the same signal processor.
E. Priority of Threads
Because the thread needs to be dispatched like a process, there must be a parameter available for dispatch, which is the thread