Now that we have malloc/free, why do we need new/delete ?, Mallocdelete

Source: Internet
Author: User

Now that we have malloc/free, why do we need new/delete ?, Mallocdelete
Zookeeper


I have some questions during the recent development process, that is, why is there new/delete in C ++ with malloc and free in C? (the irresponsible answer is that the former is in C, the latter is in C ++, huh, huh )? It is said that there has been a good research recently. In general, the difference between process-oriented and object-oriented is not accurate enough. Let's take a look:
Malloc and free are standard library functions in C/C ++, and new/delete are C ++ operators. They can be used to apply for dynamic memory and release memory.
For objects of non-Internal data types (such as their own data structure class and struct), only maloc/free functions can not meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.

Therefore, the C language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function. How does malloc/free and new/delete implement dynamic memory management of objects? I wrote a small example:


Class _ myClass {public: _ myClass (void) {cout <"Initialization" <endl ;}~ _ MyClass (void) {cout <"Destroy" <endl;} void Initialize (void) {cout <"Initialization" <endl;} void Destroy (void) {cout <"Destroy" <endl ;}; void ByeMallocFree (void) {// apply for dynamic memory. For Beginners, this guy is a bit abstract _ myClass * a = (_ myClass *) malloc (sizeof (_ myClass ));
A-> Initialize (); // initialization //... A-> Destroy (); // clear free (a); // release memory} void ByNewDelete (void ){
_ MyClass * a = new _ myClass; // apply for dynamic memory and initialize it </span>
//... Delete a; // release memory}


Have you read this example and want to say out loud that the difference is impulsive? In this example, the class _ myClass function Initialize simulates the constructor function, and the Destroy function simulates the destructor function. In the ByMallocFree function, because malloc/free cannot execute constructor and destructor, you must call the member functions Initialize and Destroy to complete initialization and clearing. Obviously, using the ByNewDelete function is very simple.
Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/delete. Because the internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to them.
The problem arises again. Since the new/delete function completely covers malloc/free, why does C not eliminate malloc/free? Haha, this is because C programs often call C functions, and C Programs can only use malloc/free to manage dynamic memory.
If you use free to release the "new Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use delete to release the "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free. If we study the underlying implementation mechanism, new/delete should be implemented based on the malloc/free mechanism.

I should have understood this time. I wish you a happy new year in 2015 and continue to struggle!

Zookeeper

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.