On C language memory management, memory leak, stack

Source: Internet
Author: User

1. Memory allocation interval:for a C language program, the memory space consists mainly of five parts: the code snippet (. Text), the data segment (. data), the static zone (. BSS), heaps, and stacks. BSS segment: BSS (BSS segment) is usually used to store uninitialized global variables and static variables in the program (note here: The general book will say that global variables and static variables are automatically initialized, then which of the uninitialized variables?) The initialization of variables can be divided into display initialization and implicit initialization, global variables and static variables if the programmer does not initialize it will be initialized, that is, no matter what type is initialized to 0, this does not show that the initialization is what we say here is uninitialized. Since they are all 0 then there is no need to store each 0, thereby saving disk space, which is the main function of BSS, a piece of memory area. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations. The BSS section does not contain any data, but simply maintains the start and end addresses, that is, the total size so that the memory area can be allocated at run time and effectively zeroed out. The BSS section does not exist in the binary image file of the application, that is, it consumes no disk space and only occupies the memory space at run time, so its executable is much smaller if the global variable and static variable are not initialized.


Data segment: Data segment usually refers to an area of memory that is used to hold the initialized global variables and static variables in the program. Data segments are static memory allocations that can be divided into read-only data segments and read-write data segments. string constants, etc., but are generally placed in read-only data segments.

Code Snippets: Code segment/text segment usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as string constants, but are generally placed in read-only data segments.

Stack area: Automatically assigned by the system, the allocation operation of the stack is built into the instruction set of the processor, which is automatically released by the system when the function execution is finished. Store local variables. The disadvantage of the stack is that the capacity is limited and local variables are no longer usable when the corresponding interval is released. command to query stack capacity: Ulimits-s. The stack is a contiguous area, extending to a high address, and the stack top and capacity are agreed in advance.         Heap Area: In the execution of the program can be assigned, by the programmer, the compiler can not allocate space for them at compile time, only when the program is run, so it is called dynamic allocation. The heap is a discontinuous area, extending to a high address. Because the system uses the linked list to describe the idle address space, the traversal of the list is from the address of the ground to the high address, so the heap area is a discontinuous dynamic storage space.
1 intA =0;//A in the global initialized data area2 Char*P1;//P1 in BSS (global variable not initialized)3 Main ()4 {5 intb//b in the stack area6 CharS[] ="ABC";//S is an array of variables, stored in the stack area,7 //"ABC" is a string constant, stored in the initialized data area8 Char*P1,P2;//P1, p2 in the stack area9 Char*P3 ="123456";//123456\0 in the initialized data area, p3 in the stack areaTen Static intc =0;//c is global (static) data, which exists in the initialized data area One //In addition, static data is automatically initialized AP1 = (Char*) malloc (Ten);//The allocated 10-byte area is in the heap area -P2 = (Char*) malloc ( -);//The allocated 20-byte area is in the heap area - Free (p1); the Free (p2); -}
2. Reasons for memory errors:(1) The memory request is unsuccessful and then used;//the use of if (p = = NULL) is often used to determine the programming;(2) The memory request succeeds, but does not initialize, can cause the memory error;(3) The memory initialization succeeds, but the operation is out of bounds, such as adding one to the operation of the array, char a [5] = "Hello", causing a segment error, without taking into account the ' \ S ' storage space, the program may not be affected if the memory space of the cross-border access is idle. If the space is already occupied, if illegal operation is performed, the program may collapse. (4) Forgetting to release memory or releasing a portion of it will cause a memory leak.  use of 3.malloc:(1) The size must be specified at the time of application;(2) Determine whether the application is successful, if not successful can not be used, otherwise it will cause memory error;(3) The return pointer is a void *, so it must be cast before it is used;(4) Explicit initialization, the contents of the heap area will not be initialized when automatically allocated (including the clear 0 operation), so the necessary initialization in the program.  use of 4.free functions:(1) in the application of memory, forget to release or release a portion, will lead to memory leaks;(2) Repeated release causes memory error;//When memory is first freed, the heap area pointed to by the pointer is disposed. At this point, it is possible for the operating system to allocate other applications to the freed heap, which destroys data from other applications when the second release occurs. (3) After the memory release is complete, the pointer is emptied (p = = NULL) Because the space that the pointer points to is freed after the free function is executed, but P is still an address value. (4) malloc must be used in pairs with free;(5) Free can only release the heap area (dynamic storage), the static zone can not be released, and the stack area.  5. Memory leaks:when the dynamically allocated memory is not in use, it should be released so that memory can be reused later. allocating memory but not releasing after use will cause a memory leak. A memory leak does not mean that there is physical disappearance, but that after the application allocates a piece of memory, because of a design error, it loses control of the memory before releasing the memory, resulting in a waste of memory. creating multiple threads in a process if you do not release Phread_join () on the thread resource, you will cause a memory leak.  memory leaks and memory usage differences: Memory leaks are memory is already occupied, but can not be reassigned to use.  6. Heap and Stack differences(1) How to apply(2) The corresponding operating system(3) Size limit of application(4) Speed of application(5) contents of heap and stack storageThe head of the heap area holds the size of the heap with one byte, and the other contents are arranged by the programmer himself;stack: When a function calls a sub-function, the first step in the stack is the address of the next executable statement of the function call statement, and then the parameters of the function into the stack, in most C compilers, the parameters of the function are from right to left one at a time into the stack, the next class is a local variable into the stack. At the end of the execution of this function, the first stack is the local variable, followed by the function parameter, and finally the address of the executable statement pointed at the top of the stack.

On C language memory management, memory leak, stack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.