Memory management summary

zhaozj2021-02-16  80

Memory management summary

Version: 0.1

Author: Soundboy

Date: 2004-07-09

modify:

Remarks: This article describes the knowledge about memory in C language development, and some special considerations in ARX development.

Basic knowledge of memory

The memory used in the process of running is mainly the following

Static memory

This part is always all of this program throughout the process, such as global data, static variables, and more.

Stack memory

The defined local variable is used by stack memory, and this part is stored in operating system management. Dynamically allocate when the program is executed, once this variable is exceeded by the scope.

Pile of memory

The above two molf memory are system management, in general, the programmer does not even feel their presence (if not active). However, the stack memory is dynamically generated by the program code, and it is also responsible for the program. So the memory function of this type is most powerful, the most flexible use, is also the most prone to problems. This part is also the focus of this article.

Pile of memory

In the C language, use the malloc () / free () function to use the heap memory. Two operators New / Delete are provided in the C language, but also can generate memory spaces, but also perform constructors and destructive functions.

Malloc / Free and New / Delete

As mentioned earlier, Malloc / Free is two functions, so it is not controlled by the compiler. NEW / Delete is two operators that can be controlled by the compiler. When the malloc () function is executed, you need to pass a length as a parameter to determine how much memory you need to assign, and the pointer returned is also no type, often requires forced to convert to some type, for example::

Char * p = (char *) malloc (sizeof (char) * 4);

When using Malloc to allocate memory, SIZEOF is often used, which can be assigned to a particular type of space is a space of this type, of course, can be used directly, but it is often inconvenient to use.

Remember to use the free () function to release this space each time you use Malloc.

Free (p) // and the above Malloc is used

NEW / Delete, because it is an operator, so the compiler knows how long the space should be allocated and what should be returned. More importantly, when implementing New, the constructor of this data type is also executed, while the DELETE is in the case, his destructor is executed. Therefore, you can use the pseudo code to describe New and Delete:

P = new type; process: if (p = malloc (sizeof (size))) {p-> constructor ()} else {p = null;} retur p; delete p; process: if (p-> ~ destructor )) {free (p);

This is very convenient to develop in object-oriented development.

Can Malloc / Free and New / DELETE use?

In general, the space assigned by malloc () is released by Free (), and the space allocated by the New is released. If you use free () to release the NEW out of space, there is no problem with the destructor. And if you use Delete to release the space out of Malloc (). It is also possible that it is wrong because it is not possible to perform a destructive function, for example:

CString * pstr = (cstring *) malloc (sizeof (cstring)); delete pstr; // Because the constructor is not executed, the destructor cannot be executed, which will cause the program to crash.

For basic types such as int, char, although interchange use will not be wrong, read will also feel confusing.

Memory leak

Memory leaks belong to a resource leak, other handles are not released, the connection is not disconnected, etc. The so-called leak is that this resource is not used, and the operating system is confirmed to be used for it. Common memory leaks have the following cases:

Only distribute, do not release

If Malloc / New is called, Free / Delete has never called, this memory is always considered to be used by the operating system. This is the most basic.

Multi-allocation, less release

Example 1

Using the New operator can allocate the space of multiple objects at once, it must be released when it is released. For example, five CHAR-shaped objects are constructed once in the code (we can regard the CHAR data type as a simple object), and use Delete P to release one. You must use Delete [] to be released.

Char * p = new char [5]; delete p; // error detete [] p; // correct

Example 2

If allocated in a loop, you must release or use a loop in the loop, which is fully reflected in the principle of New / Delete must "pair". For example, the following code, the space allocated in the FOR () loop, a total of 5 times, must be released each time:

Char * p = null; for (i = 0; i <5; i ) {p = new char; .....; delete p; // correct} delete p; // error

Or store these pointers to the final centralized release in an array in the loop.

Perform a branch release:

If the space is performed outside the branch, the release, the following programs must be performed on all branches, the following programs are released, and the function is not released, and the execution is not released, which belongs to memory leaks.

Class Aobj; AOBJ * PA = New Aobj; if (! PA-> Exce ()) {Delete Pa; ...} // is only released in the branch, and the leakage will cause other branches. Else {... // Here you should also perform release}

"Wild Space"

It is a low-level error that is not released and the memory that has not been released and has not been pointed.

Char * getastring () {char * pstr = new char [5]; // Here, the memory pstr = "abcd"; // Point PSTR to a constant address, if tracking the value of the PSTR, discovering Return PSTR }

The above situation is very horrible, because the memory is not yet, even if you want to release it, no pointer to it.

Call allocation function is not released

There are some functions that returned in the internal assigned space, and the release of the release is left to the caller of this function. E.g:

Createarect (CMYRECT * & prect);

Suppose CMYRECT is a class with a rectangle defined by our own, which constructs a heap object of a CMYRECT class, which is used by the caller and is destroyed.

In principle, this design should be avoided. If this is done, there should be a function corresponding to the corresponding deletearect (). But because ARX special database mechanics, it is a relatively common situation in ARX development. This requires us to pay great attention to the use of a new constructor.

Constant pointers do not need to be released

Since constant data is stored in a static stack memory, there is no need to deliver a program. The above, returns a string, if it is a constant type, then it is not necessary to release. Construction and release of ARX objects

In the development of Object ARX developed for AutoCAD, the use of objects with its own specific set of mechanisms, must be used with memory management of C objects. And be sure to make a clear concept, no mistakes.

ARX ​​object

Objects in ARX are also a C class object, but ARX has a set of class libraries, with its own runtime library. Therefore, there is only a specific class object that conforms to a certain condition (from several specific ARX class) is the ARX object.

database

For a .dwg file, we call it a database. There are multiple ARX objects in this database, and some must exist. Some of them are optional. There is a specific relationship between these objects.

Add to Database

In ARX, adding a database refers to associating an object in accordance with the ARX specific relationship and the original object in the database.

Several rules of ARX management objects

The addition of the database must be a heap object

That is to say, for this object, for example, CMYRECT, the object to be obtained with the following method can be added to the database.

CMYRECT * prect = New CMYRECT;

Objects to join the database cannot be released directly

According to the C memory management knowledge, there must be a delete prect; such a statement. ARX ​​is a subset of C , and of course, it is also annex. But ARX has its own mechanism to handle this matter, so that once the object refers to this pointer (a piece of memory on the heap) is added to the database, we cannot use Delete to release this object, and use ARX It is released by your own closing function.

prect-> close ();

After performing this operation, the release of the reappearance is done to AutoCAD.

Thus, for ARX development, the objects that are not added to the database are released using C delete, and the objects in the database are closing using Close (). So ask developers to stay waking up when you should be DELETE. In short, when and only when the object is added to the database, Close () must be called.

The ARX library function returns the release of the pointer

There are many functions in ARX to return a pointer, for example:

GetDescription (char * & desc) const; // returned DESC needs to release GetName (char * & name) const; // returned NAME needs to be released

To view the online documentation of these functions, there are statements like "The Caller Of this function is responsible for release the memory used by the ...", prompts the program that calls this function to release the pointer used. We should read online documents carefully when calling a function. In the comments written by the modes that the caller released, the caller is clearly required to release.

The Result Buffers pointer is released with a specific function

ARX's Result Buffers is a special linked table structure, with many functions, such as AcutbuildList (), etc., you can generate such a type of heap object, you need to use the following method to release:

Acutrelrb (Result); // Result is a RESUFFER type pointer

AR development There are other places that need to pay attention to memory management, need to be properly processed in accordance with the online documentation when specific development.

转载请注明原文地址:https://www.9cbs.com/read-14875.html

New Post(0)