From Memory Allocation

From: https://mp.weixin.qq.com/s/EyWKFRu1xryoHY386QUcuA

Before come to the question, the first statement about this number for the public there is a certain basis of developers, but the teacher will field a lucid language of some important, common technical points out explain in simple terms as possible, I would like to read, share, learn together and improve together.

         Closer to home, everyone in the usual development process, memory error occurs is a very troublesome thing, the compiler can not automatically find these errors will only be captured when the program runs, but most of these errors no obvious symptoms, hidden the time is now, to be carried out without a trace, in the face of these problems, in order to achieve the look and smell cut diagnosis, and then to cure the patient, it is better, let's start at the rationale that a thing a physical memory.

         CPU and hard disk memory is a bridge to communicate, CPU processor, the brain and core, memory and hard drive are memory, CPU-command.

         CPU operating time:

         1: the need to take out data from the memory.

         2: calculates, to keep the memory reader.

         3: calculate the results, back to the memory for storage.

If the hard drive is fast enough, you do not need memory, but the hard drive is too slow, so the work 1 and 3 as a hard disk, memory shared by the hard work 2, so that the memory is equivalent to a transit point between the hard disk and CPU .

         And we generally written procedures pre-treated (mainly the work of some code to replace text), the compiler (to generate assembly code), links (multiple object files, libraries, flattening process), generates a binary executable files stored on hard disk , when running the executable program, the operating system will execute the file, then the CPU reads the command file from the hard copy from memory to memory, CPU, an instruction to read a continuous and execute instructions.

         Memory generally includes a read only memory (ROM), a random access memory (RAM), and a cache (CACHE). The ROM is a read-only memory, a computer is generally used to store basic programs and data, such as BIOS, CPU does not exist on the ROM is read-only.

   We developed the mentioned memory means is random access memory of the computer (RAM), of a C / C ++ compiler program memory occupied by the following parts: 

(1) global area (static area): Memory at the assembly time has been good distribution, this memory during the entire operation of the program are present. Faster, less error-prone, because there will be the aftermath. Such as global variables, static variables.

(2) the stack area: automatically by the application program to the operating system assigns and recovering, fast, easy to use, but the programmer can not control. If the allocation fails, prompt the stack overflow error. During a function, the function of the local variable storage unit are created on the stack, the memory cells are automatically released at the end of the function execution. Stack memory allocation operation in the processor instruction set, high efficiency, but the limited amount of memory allocated. Stack growth area in the direction of decreasing address.

(3) heap: i.e., dynamic memory allocation. Memory program at run time using malloc or new applications of any size, the programmer is responsible when using free or delete release memory. Dynamic memory is determined by the lifetime of programmers, using very flexible. If space is allocated on the heap, we have the responsibility to recycle it, otherwise the program will run a memory leak, another frequent heap space allocation and release of different sizes will produce fragments in the stack. Note that the data structure of the heap is not the same child. Heap is extended to high-address is not continuous.

(4) a literal constant regions: constant string storage memory, after the program is released by the system.

(5) the program code area: storing binary code function body.

As shown in FIG memory allocation, the low address, high address on:

Command-line arguments and environment variables

Stack area

 

Heap

Uninitialized global area (static area)

Initialized global area (static area)

Snippet

    To understand the mechanism of memory allocation, let's look at a common memory errors and the types of responses:

 1, when using a pointer, memory allocation not successful.
  Novice programmers often make this mistake, because they do not realize that memory allocation will be unsuccessful. Common solution is to check before using the memory pointer is NULL. If the pointer p is a function of the parameters, then check with the assert (p! = NULL) at the entrance of the function. If you are using malloc or new to allocate memory, error protection should be used if (p == NULL) or if (p! = NULL).
2, although the memory allocation successful, but has not been initialized to reference it.
  Make this mistake has two main causes: First, there is no concept of initialization; the second is the misconception that the whole memory of the default initial value is zero, leading to the initial reference error (such as arrays).
The default initial value memory of what is no uniform standard, though sometimes zero value, we prefer to believe that there is no credible. So regardless of the manner in which the array is created, do not forget to have initial value, even assigned a value of zero can not be omitted, not too troublesome.
3, memory allocation is successful and has been initialized, but the operation crossed the border memory.
   For example, frequently occurs when using the subscript array operations "over" or "low 1". Especially in the for loop statement, it is easy to mistake the number of cycles, resulting in an array of cross-border operations.

4, forget the free memory, causing memory leaks.
   This function contains the error is called every time a memory is lost. Ample memory when the system started, you can not see the error. Once the program eventually died suddenly, the system prompts: memory exhausted. Dynamic application must be paired with the release of the memory, the number of malloc and free use of the program must be the same as, or certainly an error (new / delete empathy).
5, the release of memory but continue to use it.
There are three cases:
(1) the relationship between objects in the program call is too complicated, it is difficult to figure out whether an object is actually the memory has been released, this time should be redesigned data structure to solve the chaos Object Management fundamentally.
(2) return statement in the function's wrong, be careful not to return to point to "stack memory" and "pointer" or "reference", because the memory is automatically destroyed at the end of the function body.
(3) release using free or delete memory, the pointer is not set to NULL. Resulting in "wild pointer."

Attachment: the malloc and new distinction:

(1) new, delete operator is to be overloaded, can only be used in C ++. 

(2) malloc, free function is to be covered, C, C ++ may be used in both. 

(3) new call constructors object, call the appropriate corresponding delete destructor. 

(4) malloc allocate memory only, free only to reclaim memory, and does not perform configuration destructor 

(5) new, delete the returned pointer is a data type, malloc, free void pointer is returned.

Guess you like

Origin blog.csdn.net/u013755520/article/details/91494140