Joke Collection Website - Mood Talk - C language knowledge: Talk about the differences between the following statements

C language knowledge: Talk about the differences between the following statements

char *a = "abc" is stored in the constant area or static storage area. This is related to the compiler. It is treated as a constant during compilation, which is equivalent to the const type; char b[] = "abc" is stored in the stack storage area. , not in the heap storage area; char c[3] = "abc" is stored in the heap storage area. The heap storage area is generally obtained by applying for memory, such as new operation.

The following explains the three ways in which C language is related to memory allocation:

(1) Allocate from static storage area. Memory is allocated when the program is compiled, and this memory exists throughout the entire runtime of the program. For example, global variables and static variables.

(2) Created on the stack. When executing a function, the storage units for local variables within the function can be created on the stack. These storage units are automatically released when the function is executed, following the first-in, last-out principle. The stack memory allocation operation is built into the processor's instruction set and is very efficient, but the allocated memory capacity is limited. This method does not produce memory fragmentation, and programmers do not need to intervene in memory allocation.

(3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, you can use malloc or new to apply for any amount of memory. The programmer is responsible for when to use free or delete to release the memory. The dynamic memory allocation method requires programmer intervention and will produce memory fragmentation. Its lifetime is determined by us. It is very flexible to use, but it also has the most problems.