Possible memory errors in the C ++ program are summarized as single orders.

Source: Internet
Author: User

This article mainly summarizes the memory errors that may occur in the C ++ program. It is extracted after reading the use and analysis of rational purify. I believe that many junior C ++ programmers have been confused by these problems just like me, and I hope they will be helpful to the readers.

I. Classification of Memory Errors
A. Memory Access Error
An error occurs when the memory is read or written. It may be a memory unit that has not been initialized, or a memory unit that has been read or written incorrectly.
B. memory usage Error
This error occurs when the memory is not correctly released after the dynamic request.

Ii. Memory analysis (typical C ++ Memory Model)

 

BSS segment: BSS segment (BSS segment) is usually a memory area used to store uninitialized global variables in the program. BSS is short for block started by symbol. BSS segments belong to static memory allocation.

Data Segment: a data segment is usually a memory area used to store initialized global variables in the program. The data segment belongs to the static memory allocation. (In fact, I don't quite understand why we manage initialized and uninitialized segments since they all store global variables)

Code segment: A code segment (code segment/Text Segment) is usually a memory area 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. Some architectures also allow code segments to be writable, that is, programs can be modified. In the code segment, it may also contain some read-only constant variables, such as string constants.

Heap: a heap is used to store the memory segments dynamically allocated during a process. Its size is not fixed and can be dynamically expanded or reduced. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap is expanded). When a function such as free is used to release the memory, released memory is removed from the heap (the heap is reduced)

STACK: A stack, also known as a stack, is a local variable temporarily created by the user to store the program. That is to say, the variable defined in the callback arc "{}" (but does not include static declared variables, static means to store variables in data segments ). In addition, when a function is called, its parameters will also be pushed into the process stack that initiates the call. After the call is completed, the return values of the function will also be stored in the stack. Because of the stack's first-in-first-out feature, the stack is particularly convenient for storing/restoring the call site. In this sense, we can regard the stack as a memory zone for storing and exchanging temporary data.

C ++ is different from C # and Java in that it can dynamically manage the memory, but it cannot have both of them, the cost of flexibility is that programmers need to spend more energy to ensure that the Code does not encounter memory errors.

Iii. Common memory access errors and memory usage errors

Specifically, memory access errors include the following: uninitialized memory units, array access errors, invalid memory units (0x000000, 0x000005, etc.), and invalid write memory.

Memory usage errors include: 1. The memory is not released after the request, which can be avoided by pairing new and delete. 2. Release a piece of memory and then release it again.

Iv. Example

1 # include <iostream> 2 using namespace STD; 3 int main () {4 char * str1 = "four"; 5 char * str2 = new char [4]; // not enough space6 char * str3 = str2; 7 cout <str2 <Endl; // umr8 strcpy (str2, str1 ); // abw9 cout <str2 <Endl; // abr10 Delete str2; 11 str2 [0] + = 2; // FMR and fmw12 Delete str3; // ffm13} UMR: uninitialized memery read. read uninitialized memory ABW: array bound write. FMR/W: freed memery read/write. read/write released memory FFM: free FR EED memery. release the released memory from the above programs. We can see that when the memory is allocated in Row 3, the space occupied by the string Terminator "/0" is ignored, resulting in out-of-bounds array write (Array Bounds Write) and out-of-bounds array read (Array bounds read) of the 8th rows ); in row 3, printing str2 that has not been assigned a value will generate an uninitialized memory access error (uninitialized memory read ); using released variables in Row 3 will lead to errors in releasing memory read and write (freed memory read and freed memory write). Finally, because str3 and str2 refer to the same memory, row 3 releases the space that has been released again (free freed memory ).

This program contains many errors and can be compiled and connected and run on many platforms. However, these errors are like time bombs that will be triggered under special configurations, resulting in unforeseen errors. This is one of the main reasons why memory errors are hard to be found.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/FuXiaoZhuan/archive/2008/10/18/3096839.aspx

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.