Clause 3: Try to use new and delete instead of malloc and free

Source: Internet
Author: User
Clause 3: Try to use new and delete instead of malloc and free

 

Malloc and free (and their variants) cause problems because they are too simple: they do not know constructors and destructor.

Assume that two methods are used to allocate space to an array containing 10 string objects. One uses malloc and the other uses new:

  

string *stringarray1 =static_cast<string*>(malloc(10 * sizeof(string)));string *stringarray2 = new string[10];

The result is that stringarray1 does point to enough space for 10 string objects, but these objects are not created in the memory. Moreover, if you do not jump out of this obscure syntactic cycle (For details, refer to the terms M4 and M8), You Cannot initialize the objects in the array. In other words, stringarray1 is actually useless at all. Instead, stringarray2 points to an array containing 10 fully constructed string objects. Each object can be safely used in any string read operation.

Suppose you think of a strange trick to initialize the objects in the stringarray1 array, then you will certainly do the following in your program:

 

Free (stringarray1); Delete [] stringarray2; // For more information, see clause 5. Why do I add "[]" here?

Calling free will release the memory directed to stringarray1, but the string object in the memory will not call the destructor. If the string object has allocated the memory as usual, all the memory will be lost. On the contrary, when calling Delete For stringarray2, each object in the array will call the Destructor before the memory is released.

Since new and delete can interact effectively with constructors and destructor, it is obvious to choose them.

Mixing new and delete with malloc and free is also a bad idea. It is unpredictable to call free for a pointer obtained with new or delete for a pointer obtained with malloc. Everyone knows what "unpredictable" means: it may work well in the development phase and in the testing phase, but it may end up on the face of your most important customers.

Incompatibility between new/delete and malloc/free often results in some serious complexity problems. For example, <string. h> usually has a strdup function. It gets a char * string and returns its copy:

 

Char * strdup (const char * PS); // returns the copy indicated by PS.

In some cases, C and C ++ use the same strdup version, so the function uses malloc to allocate memory. In this case, Some uninformed C ++ programmers will ignore the need to perform the free operation on the pointer returned by strdup after calling strdup. To prevent this, strdup will be specifically rewritten for C ++ in some cases, and new is called inside the function. Therefore, the caller must remember to use Delete. As you can imagine, this can lead to a serious portability problem, because strdup in the code is in different forms in different places.

C ++ programmers and C programmers are very interested in code reuse. As we all know, a large number of C libraries written based on malloc and free are worth reuse. When using these databases, it is best that you do not need to free up the memory of the database's own malloc, and/or, you do not need to go to the malloc Library and the memory will be free up, which is great. In fact, there is no error in using malloc and free in the C ++ program, as long as the pointer obtained by using malloc is free, or the pointer obtained by using new is finally operated by Delete. Never mix new with free, malloc, or delete.

Since malloc and free do not know anything about constructor and destructor, mixing malloc/free and new/delete is as difficult to control as a noisy party, you 'd better use new and delete with one mind at any time.

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.