Effective C ++ 2e Item3

zhaozj2021-02-11  186

Terms 3: Try to use New and Delete without Malloc and Free

The cause of Malloc and Free (and its variants) will have a problem that they are too simple: they don't know constructor and destructor.

Assume that two methods give an array allocated space containing 10 String objects, one with malloc, another new: string * stringArray1 = static_cast (malloc (10 * sizeof (string));

String * stringArray2 = new string [10];

As a result, StringArray1 does point to sufficient space that can accommodate 10 String objects, but these objects are not created in memory. Moreover, if you don't jump from this obscure symbol (see the description of clauses M4 and M8), you have no way to initialize the objects in the array. In other words, StringArray1 is actually not used. Instead, StringArray2 points to an array containing 10 fully constructive String objects, each of which can be safely used in any read string.

Suppose you want a strange trick to initialize the objects in the StringArray1 array, then you will do this in your later program: Free (StringArray1);

DELETE [] StringArray2; // See the Terms 5: Why do you have a "[]"

Calling Free will release the memory pointing by StringArray1, but the String object in memory does not call the destructor. If the String object is in general, you have allocated memory, and these memory will be lost. Conversely, each object in the array will call the destructor before memory release when calling DELETE.

Since New and Delete can be interactively interact with constructor and destructive functions such as constructor, they are obvious.

It is also a bad idea to mix new and delete with Malloc and Free. Call free with a pointer for NEW, or call Delete to call with Malloc, which is unpredictable. Everyone knows "unpredictable" means that it may work well in the development phase, work well during the test phase, but may also be in the face of your most important customer's face.

The incompatibility between New / Delete and Malloc / Free often leads to some serious complexity issues. For example, it usually has a strDup function in , it gets a char * string and then returns its copy: char * strdup (const char * ps); // Return to the copy of the PS

In some places, C and C use the same STRDUP version, so the function is allocated with Malloc. In this case, some uninformed C programmers will ignore the pointer returned to the strDUp for free operation after calling StrDUP. In order to prevent this, some places will rewrite StrDup for C , and new NEW is called inside the function, which requires its caller to remember the last use of Delete. You can imagine, this will cause how serious transplantability, because StrDUP in the code is in different places in different places.

C programmers and C programmers are also very interested in reused code. Everyone knows that there is a large number of C libumes composed of codes written by Malloc and Free. When using these libraries, it is best that you don't have to be responsible for going to FREE to drop your own Malloc's memory, and / or you don't have to go to the MalloC library you will fall by Free, this is great. In fact, use Malloc and Free in the C program, just guarantees that the pointer to Malloc is used to use free, or the pointer you get with New is finally used to operate with Delete. Don't use new and free or malloc and delete, it will only find trouble. Since Malloc and Free don't know about the constructor and the destructive function, mix Malloc / Free and New / Delete as uncontrolled as a noisy congestion party, then, when you are best to use it New and delete.

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

New Post(0)