Computer Memory Management

Source: Internet
Author: User

With the in-depth study of C/C ++, it is necessary for us to have a certain understanding of computer memory management so that we can use these advanced programming languages freely. Here we will discuss the knowledge about memory management.

1. Memory Allocation Method

The memory occupied by a C/C ++ compiled program is divided into the following parts:

1. stack)-the compiler automatically allocates the release, stores the function parameter values, local variable values, and so on. The operation method is similar to the stack in the data structure.

2. heap in the heap area)-generally, it is assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the operating system OS when the program ends. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.

3. The Global static zone) is static. global variables and static variables are stored in one partition. initialized global variables and static variables are stored in one partition, uninitialized global variables and uninitialized static variables are in another adjacent area. -The System Automatically releases the program after it ends.

4. Text constant data) area-constant strings are placed here. The system automatically releases the program after it ends.

5. program code area-stores the binary code of the function body.

2 Common Errors in memory management

Common memory management errors and their countermeasures are as follows:

1) memory allocation fails, but it is used.

New programmers often make this mistake because they do not realize that memory allocation will fail. A common solution is to check whether the pointer is NULL before using the memory. If the pointer p is a function parameter, use assert (p! = NULL. If you use malloc or new to apply for memory, you should use if (p = NULL) or if (p! = NULL.

2) Although the memory allocation is successful, it is referenced before initialization.

There are two main causes for this mistake:

First, there is no idea of initialization;

The second is to mistakenly assume that the default initial values of the memory are all zero, resulting in incorrect reference values, such as arrays ).

There is no uniform standard for the default initial values of the memory. Although sometimes it is zero, we prefer to trust it without any trust. Therefore, no matter which method is used to create an array, do not forget to assign the initial value, even if it is null, it cannot be omitted. Otherwise, your code will easily hide bugs.

3) memory allocation is successful and initialized, but the operation is beyond the memory boundary.

For example, when an array is used, the subscript "more than 1" or "less than 1" is often performed. Especially in for loop statements, the number of loops is easy to make a mistake, resulting in array operations out of bounds.

4) forgot to release the memory, causing memory leakage.

A function containing such errors loses a piece of memory every time it is called. This is too generous! It is very valuable to know the computer's memory space! At the beginning, the system had sufficient memory and you could not see the error. Once a program suddenly died, the system prompts: memory is exhausted.

Dynamic Memory application and release must be paired, and the usage of malloc and free in the program must be the same, otherwise there must be a new/delete error ).

5) The memory is released, but it is still used.

There are three scenarios:

1) The object calling relationship in the program is too complex, so it is difficult to figure out whether an object has released the memory. At this time, we should re-design the data structure to fundamentally solve the chaos of Object Management.

2) The return Statement of the function is incorrect. Be sure not to return the "Pointer" or "Reference" pointing to "stack memory" because the function body is automatically destroyed when it ends.

3) after free or delete is used to release the memory, the pointer is not set to NULL. As a result, a "wild pointer" is generated ".

6) The pointer is forcibly converted to an unmatched data structure.

Memory usage rules

Rule 1: After applying for memory with malloc or new, check whether the pointer value is NULL immediately. Prevents the use of memory with NULL pointer values.

Rule 2: Do not forget to assign initial values to arrays and dynamic memory. Avoid using uninitialized memory as the right value.

Rule 3: avoid overrunning the subscript of an array or pointer. Be careful when "more than 1" or "less than 1" is performed.

Rule 4: dynamic memory application and release must be paired to prevent memory leakage.

Rule 5: After the memory is released with free or delete, the pointer is immediately set to NULL to prevent "wild pointer ".

3 Comparison Between pointers and Arrays

In C ++/C Programs, pointers and arrays can be replaced with each other in many places, which leads to the illusion that the two are equivalent, but the fact is that there is a fundamental difference between the two.

An array is created either in a static storage area, such as a global array, or on a stack. The array name corresponds to rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the lifetime, only the content of the array can be changed.

A pointer can point to any type of memory block at any time, and its feature is "variable". Therefore, we often use pointers to operate dynamic memory.

The pointer is far more flexible than the array, but it is also more dangerous. If you do not pay attention to it, it will lead to troubles if you make a mistake! Therefore, exercise caution when using pointers. So far, we will continue to summarize and summarize the problems related to the computing memory.

Note: a considerable amount of information is written after I find and learn from other advanced documents on the Internet! ^-^


This article from the "road to growth" blog, please be sure to keep this source http://7905648.blog.51cto.com/7895648/1295016

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.