Go: How memory is allocated and common errors

Source: Internet
Author: User

Memory operations are always a minefield for program developers. In this area, there is always a constant mine, mine clearance, and often a very high cost of demining. Remember when the company internship, there are about 2 weeks, the team of several people to mine--there is a place memory leak!

As far as I can see, only a handful of programer dare to clap their breasts and say, very familiar with memory operations, extremely easy will not mine. In response to this situation, I asked myself to explore as much as possible in order to bury the Thunder less and not even bury mine (this is a big challenge). The following is excerpted from the High quality C++/C Programming Guide (Author: Dr. Lin Rui) to alert yourself. More detailed, in-depth things to keep me in-depth investigation, study.

1, memory allocation method

There are three ways to allocate memory:

(1) Allocation from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.

(2) Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

(3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by us and is very flexible to use, but the problem is the most.

2. Common memory errors and Countermeasures

A memory error is a very troublesome thing to happen. These errors are not automatically discovered by the compiler and are usually captured when the program is running. And most of these errors are not obvious symptoms, and the hidden, increased the difficulty of error. Sometimes the user angrily to find you, the program did not have any problems, you go, wrong and attack.

Common memory errors and their countermeasures are as follows:

    • The memory allocation was unsuccessful, but it was used.

Novice programmers often make this mistake because they are unaware that the memory allocation will not succeed. A common workaround is to check if the pointer is null before using memory. If the pointer p is an argument to a function, it is checked with an assert (P!=null) at the entrance of the function. If you are using malloc or new to request memory, you should use if (p==null) or if (P!=null) for error-proof handling.

    • The memory allocation succeeds, but it is not initialized to reference it.

There are two main causes of this error: one is the idea of no initialization, and the other is to mistakenly assume that the default initial value of the memory is all zero, resulting in a reference to the initial error (for example, an array).

There is no uniform standard for what the default initial value of memory is, although sometimes it is a zero value and we would rather believe it to be credible. So no matter how to create an array, do not forget to assign the initial value, even if it is assigned 0 values can not be omitted, do not bother.

    • The memory allocation succeeds and has been initialized, but the operation crosses the memory boundary.

For example, the use of arrays often occurs when the subscript "more 1" or "less 1" operation. Especially in a For loop statement, the number of loops can be easily mistaken, resulting in array operations being out of bounds.

    • Forgot to release memory, causing a memory leak.

The function that contains this error loses one piece of memory each time it is called. At first, the system has plenty of memory and you can't see the error. One time the program suddenly died, the system appears prompt: memory exhaustion.

Dynamic memory application and release must be paired, the program malloc and free use must be the same number, otherwise there must be errors (new/delete).

    • Frees up memory but continues to use it.

There are three types of cases:

(1) The object call relationship in the program is too complex, it is difficult to know whether an object has freed the memory, at this time should redesign the data structure, fundamentally solve the chaos of object management.

(2) The return statement of the function is incorrectly written, and be careful not to return a pointer or reference to "stack memory" because the memory is automatically destroyed at the end of the function body.

(3) After releasing memory with free or delete, the pointer is not set to null. Causes the "wild pointer" to be produced.

"Rule 1 after requesting memory with malloc or new, you should immediately check that the pointer value is NULL. Prevents the use of memory with a pointer value of NULL.

"Rule 2 " do not forget to assign an initial value to both arrays and dynamic memory. Prevents memory that is not initialized from being used as the right value.

"Rule 3 " avoid array or pointer subscript out of bounds, especially beware of" more than 1 "or" 1 less "operations.

"Rule 4 " dynamic memory requests and releases must be paired to prevent memory leaks."

"Rule 5 " when memory is freed with free or delete, the pointer is immediately set to NULL to prevent the" wild pointer "from being produced.

Source: http://www.cnblogs.com/skynet/
This article is based on the attribution 2.5 China mainland license agreement, which is reproduced, interpreted or used for commercial purposes, but must retain the attribution Wu Qin (including links) herein.

Go: How memory is allocated and common errors

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.