Directory:

  1. Preparatory knowledge – program memory allocation
  2. The sample code
  3. How are global variables stored in memory, local variables, static global variables, and static local variables stored in memory
  4. The difference between heap and stack redistribution
  5. Why manage memory
  6. Memory Management mode

1. Preliminary knowledge – program memory allocation

The memory footprint of a C/C++ compiled program is divided into the following sections

  • Stack – Automatically allocated by the compiler to hold function parameter values, local variable values, and so on. It operates like a stack in a data structure.
  • Heap – Usually allocated and released by the programmer. If not released by the programmer, the program may be collected by the OS at the end of the program. Note that this is not the same thing as a heap in a data structure, but is distributed like a linked list.
  • Global (static) — Global and static variables are stored together. Initialized global and static variables are stored in one area. Uninitialized global and uninitialized static variables are stored in another area adjacent to each other. – Released by the system after the program ends.
  • Literal constants – This is where constant strings are placed. The program is released by the system after completion
  • Program code area – the binary code that holds the function body.

2. sample code

int a = 0;      // Global initialization area
char *p1;      // Global uninitialized area

int main() {
    int b;            / / the stack area
    char s[] = "abc"; / / the stack area
    char *p2; / / the stack area
    char *p3 = "123456"; // p3 is in the stack; "123456\0" in the constant area,
    
    static int c =0;      // Global (static) initialization area
    p1 = (char *)malloc(10);
    p2 = (char *)malloc(20); // The allocated 10 - and 20-byte areas are in the heap
    strcpy(p1, "123456");    // "123456\0" is placed in the constant area. The compiler may optimize it to the same place as "123456" pointed to by p3.
}
Copy the code

3. How are global variables, local variables, static global variables, and static local variables stored in memory? What is the difference

In terms of scope:

  • ** Global variables: ** has global scope. Global variables need only be defined in one source file to apply to all source files. Of course, other source files that do not contain global variable definitions need to declare the global variable again with the extern keyword.
  • ** Local variables: ** also has only local scope, it is an auto object, it does not always exist during the execution of the program, but only during the execution of the function, after the completion of a call to the function, the variable is destroyed, its memory is reclaimed.
  • ** Static local variables: ** has local scope. It is initialized only once, and lasts from the first time it is initialized until the end of the program. The difference between global variables and global variables is that global variables are visible to all functions, while static local variables are always visible to the function body that defines them.
  • Static global variables: they have a global scope. If a program contains more than one file, they apply to the file that defines them, not to other files. This way, even if two different source files define static global variables with the same name, they are still different variables.

From the perspective of allocating memory space: global variables, static local variables, static global variables are allocated space in the static storage area, and local variables are allocated space in the stack.

From the above analysis, it can be seen that changing a local variable into a static variable changes its storage mode, that is, its lifetime. Changing a global variable to a static variable changes its scope and limits its use. So the static specifier works differently in different places.

In general, the difference between global variables, local variables, static global variables, and static local variables is

  • Different life cycle
  • Different scope of action
  • Different ways of distribution

4. The difference between heap and stack redistribution

Stack: Automatically allocated by the system. For example, declare a local variable int b in a function; P1 = (char *)malloc(10); p1 = (char *)malloc(10); P2 = (char *)new(10); But notice that P1 and P2 are themselves on the stack.

5. Why manage memory

90% of iOS applications Crash because of memory problems. In a project with dozens or even hundreds of classes, it is extremely difficult to find memory problems. Learning memory management can help us reduce the probability of errors.

The memory problem is reflected in two —– memory overflow, wild pointer exception

  • Internal overflow iOS allocates a certain amount of memory for each application to run. Once the memory limit is exceeded, the program crashes.

  • The memory of the wild pointer exception object has been reclaimed by the system, but the pointer is still used to manipulate the memory. Wild pointer exception is one of the important causes of program Crash. The larger the code is, the more likely the program will have the problem of wild pointer.

6. Memory Management mode