C + +: memory allocation

Source: Internet
Author: User

System blue screen, a large part of the system itself is caused by the code defects, and the system code defects are largely related to improper memory allocation. Understanding memory allocation is necessary for a qualified program, because of problems such as stack overflow, buffer overflow, and so on due to improper memory allocation, which often leads to system paralysis and even crashes.

    • form of memory allocation
    • Memory leaks
    • Buffer overflow

form of memory allocation

The system memory used by a program compiled by C/+ + is generally divided into the following sections:

(1) a block (Block Started by SYMBOL,BSS) segment with a symbolic start: a BSS segment usually refers to an area of memory that is used to hold uninitialized global data and static data in the program. BSS segments are static memory allocations, and static variable resources are automatically released by the system after the program ends.

(2) Data segment: Data segments typically refer to a piece of memory that is used to hold the initialized global variables in the program. Data segments also belong to static memory allocations.

(3) Snippet (code segment/text segment): Code snippets are sometimes called text segments, usually refers to a block of memory that is used to store program execution code (exposing class member functions and global functions and other function code), and the size of this area is determined before the program is run. And the memory area is usually read-only. In a code snippet, it is also possible to include some read-only constant variables, such as String constants. This section is generally shared, such as the Linux system opened two VI to edit the text, then generally these two VI is shared a code snippet.

(4) Heap: A heap is used to store dynamically allocated memory segments in a process run, which are not fixed and can be dynamically expanded or scaled down. When a process calls a function such as malloc or new to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded), and when the memory is freed using functions such as free or delete, the newly allocated memory is dynamically added to the heap (heap is expanded). When freeing memory with functions such as free or delete, the freed memory is removed from the heap (heap is scaled down). The heap is typically released by the programmer, and if the programmer does not release it, the program may end up being recycled by the operating system

(5) Stack: A local variable that is temporarily created by the stack user store program, typically including variables defined in the function brackets (excluding static declared variables, static means storing variables in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call is finished. The stack is automatically freed by the compiler, storing the function's parameter values, local variable values, and so on. It operates in a manner similar to a stack in a data structure. The stack memory allocation operation is built into the processor's instruction set, generally using registers to access, high efficiency, but the allocated memory capacity is limited.

Note: There is a clear separation between the code snippet and the data segment, but there is no data segment and stack segment, and the stack is growing downward, and the heap is growing upward.

Memory leaks

The heap is dynamically allocated memory, and can be allocated using very large memory, which produces a memory leak when not in use. Frequent use of malloc and free generates memory fragmentation (similar to disk fragmentation).

Memory leak is a situation in which a program fails to release memory that is no longer in use due to negligence or error. Commonly said memory leak refers to the heap memory leakage, memory leaks in fact not refers to the existence of physical disappearance, but the application allocation of memory, due to design errors, lost control of the memory, resulting in a waste of memory. Memory leaks have similar symptoms to many other problems, and usually can only be analyzed by Cheng that can obtain program source code.

Applications typically use functions such as malloc, Calloc, ReAlloc, and new to allocate a chunk of memory from the heap, and after use, the program must be responsible for calling free or delete to release the memory block, otherwise the memory cannot be reused, causing a memory leak

Memory leaks often cause the system to run out of CPU resources. , so developers need to develop good programming habits during coding, and the memory allocated with malloc or new should be freed with free or delete as appropriate, ensuring that no memory location becomes orphaned until the pointer is assigned a value. Whenever a structured element is released, and the element contains a pointer to a dynamically allocated memory location, the function return value that returns the dynamically allocated memory reference should be traversed first from the memory location and released from there and then traversed back to the parent node, always correctly handled

Buffer overflow

A buffer is a contiguous block of machine memory at the time the program runs, which holds the data for a given type, and problems occur with dynamically assigned variables. A buffer overflow is when the number of data bits that are filled into the buffer exceeds the capacity limit of the buffer itself, and the overflow data that occurs is overwritten with legitimate data (data, pointers to the next instruction, function return address, and so on). The best scenario is that the program does not allow the input of characters that exceed the length of the buffer and check the length of the data, since most programs assume that the data length is always equal to the allocated storage space, and there is a buffer overflow security risk.

C + +: memory allocation

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.