1. Conceptual interpretation
Static storage: Within the time the program is compiled, it is already allocated, and the entire running period of the program exists in this block. It mainly stores static data, global data, and constants.
Stack area: When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.
Heap area: Also known as dynamic memory allocation. The program uses malloc or new to request any size of memory at run time, and the programmer is responsible for freeing the memory with free or delete when appropriate. The lifetime of dynamic memory can be determined by us, and if we do not release the memory, the program will release the dynamic memory at the end. However, good programming habits are: If a dynamic memory is no longer used, it needs to be released, otherwise, we think there is a memory leak phenomenon.
2. The difference between the code analysis
Example one: static storage and Stack area
char* p = "Hello World1";
Char a[] = "Hello World2";
P[2] = ' A ';
A[2] = ' A ';
char* p1 = "Hello World1;"
This program is wrong,
This program is wrong, the error occurs in p[2] = ' A ' line of code, because the data "Hello World2" exists in the array, so this data is stored in the stack area, there is no problem with its modification. Because the pointer variable p is only able to store the address of a storage space, the data "Hello World1" is a string constant, so it is stored in a static storage area. Although through p[2] you can access the third unit of data in the static store, the unit of storage where the character ' L ' resides. However, because the data "Hello World1" is a string constant, it cannot be changed, so a memory error is reported when the program is run. Also, if the output of P and P1 at this time, it will be found that the address stored in P and P1 is exactly the same. In other words, only one copy of the same data is kept in the data area.
Example two: Stack area and heap area
char* F1 ()
{
char* p = NULL;
Char A;
p = &a;
return p;
}
char* F2 ()
{
char* p = NULL:
p = (char*) new char[4];
return p;
}
Both of these functions return the address of a storage space, what is the difference between them? The F1 () function returns a storage space, but this space is a temporary space. In other words, this space has only a short life cycle, and its life cycle at the end of the function F1 () call loses its life value, namely: this space is freed. So, when you call the F1 () function, if you have the following statement in your program:
Char* p;
p = F1 ();
*p = ' a ';
At this point, the compilation does not report an error, but an exception error occurs when the program is run. Because you're working on memory that should not be manipulated (that is, the storage space that has been freed). However, in contrast, the F2 () function does not have any problems. Because the new command is to request storage space in the heap, once the application succeeds, the memory will persist unless you delete it or terminate the program. It can also be understood that heap memory is a shared unit that can be accessed by multiple functions together. Heap memory is a good choice if you need to have more than one data return but no way.
However, you cannot use temporary variables to hold the first address of the heap memory, because once the temporary variable is released, the memory will not be able to be called, causing a memory leak.
iOS notes-static storage, heap, stack