Let's talk about new and malloc in C ++.

Source: Internet
Author: User

From http://blog.csdn.net/piaojun_pj/article/details/5979819

 

 

Difference between new and malloc:

 

1. New is the operator in C ++, and malloc is a function in C.

 

2. New not only allocates memory, but also calls class constructor. Similarly, delete calls class destructor, while malloc only allocates memory, does not initialize class members, or free does not call destructor.

 

3. Memory leakage can be checked out for malloc or new. The difference is that new can indicate the row of the file, but malloc does not have this information.

 

4. Comparison of new and malloc Efficiency

New can be considered as the execution of the malloc and constructor.

The new pointer directly carries the type information.

Malloc returns the void pointer.

 

1. New Delete is an operator, malloc and free are functions.

Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory. For non-Internal data objects, maloc/free alone cannot 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. Let's first take a look at how malloc/free and new/delete implement dynamic memory management of objects, as shown in the example.

 

[CPP]
View plaincopyprint?
  1. ClassOBJ
  2. {
  3. Public:
  4. OBJ (Void) {Cout <"initialization" <Endl ;}
  5. ~ OBJ (Void) {Cout <"destroy" <Endl ;}
  6. VoidInitialize (Void) {Cout <"initialization" <Endl ;}
  7. VoidDestroy (Void) {Cout <"destroy" <Endl ;}
  8. };
  9. VoidUsemallocfree (Void)
  10. {
  11. OBJ * A = (OBJ *) malloc (Sizeof(OBJ); // apply for dynamic memory
  12. A-> initialize (); // Initialization
  13. //...
  14. A-> destroy (); // clear the job
  15. Free (a); // releases the memory.
  16. }
  17. VoidUsenewdelete (Void)
  18. {
  19. OBJ * A =NewOBJ; // apply for dynamic memory and initialize
  20. //...
  21. DeleteA; // clear and release the memory
  22. }

Class OBJ <br/>{< br/> Public: <br/> OBJ (void) {cout <"initialization" <Endl ;}< br/> ~ OBJ (void) {cout <"Destroy" <Endl ;}< br/> void initialize (void) {cout <"initialization" <Endl ;} <br/> void destroy (void) {cout <"Destroy" <Endl ;}< br/>}; </P> <p> void usemallocfree (void) <br/>{< br/> OBJ * A = (OBJ *) malloc (sizeof (OBJ )); // apply for dynamic memory <br/> A-> initialize (); // initialize <br/> //... <br/> A-> destroy (); // clear the job <br/> free (); // release memory <br/>}</P> <p> void usenewdelete (void) <br/>{< br/> OBJ * A = new OBJ; // apply for dynamic memory and initialize it <br/> //... <br/> Delete A; // clear and release the memory <br/>}

In this example, how to use malloc/free and new/Delete to implement the object's dynamic memory management class OBJ function initialize to simulate the constructor function, and destroy to simulate the destructor function. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. The usenewdelete function is much simpler. 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. Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? 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.

 

Ii. New Delete actually calls the malloc and free functions in implementation.

 

3. In addition to memory allocation, new operator also calls constructors. The malloc function is only responsible for allocating memory.

 
New one-dimensional array:

Char * arr;
Int Len; // dynamically determine the length value

Arr = new char [Len]; // Dynamic Allocation, you can also use malloc
...
Delete [] arr; // do not forget to release

New multi-dimensional array:

The correct method is to declare an n-dimensional array. Each unit is a pointer to a char, and then each unit is allocated separately.

Memory. The Code is as follows:

Char ** array = new char * [N];
For (INT I = 0; I <n; I ++)

Array [I] = new char [m];

 

Note: pay special attention to the above Code when releasing the allocated memory. Because this is "deep memory allocation ",

To release the memory pointed to by the pointer in each unit. The code for releasing memory is as follows:

For (I = 0; I <n; I ++)

Delete [] array [I];
Delete [] array;

From http://blog.csdn.net/piaojun_pj/article/details/5979819

 

 

Difference between new and malloc:

 

1. New is the operator in C ++, and malloc is a function in C.

 

2. New not only allocates memory, but also calls class constructor. Similarly, delete calls class destructor, while malloc only allocates memory, does not initialize class members, or free does not call destructor.

 

3. Memory leakage can be checked out for malloc or new. The difference is that new can indicate the row of the file, but malloc does not have this information.

 

4. Comparison of new and malloc Efficiency

New can be considered as the execution of the malloc and constructor.

The new pointer directly carries the type information.

Malloc returns the void pointer.

 

1. New Delete is an operator, malloc and free are functions.

Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory. For non-Internal data objects, maloc/free alone cannot 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. Let's first take a look at how malloc/free and new/delete implement dynamic memory management of objects, as shown in the example.

 

[CPP]
View plaincopyprint?
  1. ClassOBJ
  2. {
  3. Public:
  4. OBJ (Void) {Cout <"initialization" <Endl ;}
  5. ~ OBJ (Void) {Cout <"destroy" <Endl ;}
  6. VoidInitialize (Void) {Cout <"initialization" <Endl ;}
  7. VoidDestroy (Void) {Cout <"destroy" <Endl ;}
  8. };
  9. VoidUsemallocfree (Void)
  10. {
  11. OBJ * A = (OBJ *) malloc (Sizeof(OBJ); // apply for dynamic memory
  12. A-> initialize (); // Initialization
  13. //...
  14. A-> destroy (); // clear the job
  15. Free (a); // releases the memory.
  16. }
  17. VoidUsenewdelete (Void)
  18. {
  19. OBJ * A =NewOBJ; // apply for dynamic memory and initialize
  20. //...
  21. DeleteA; // clear and release the memory
  22. }

Class OBJ <br/>{< br/> Public: <br/> OBJ (void) {cout <"initialization" <Endl ;}< br/> ~ OBJ (void) {cout <"Destroy" <Endl ;}< br/> void initialize (void) {cout <"initialization" <Endl ;} <br/> void destroy (void) {cout <"Destroy" <Endl ;}< br/>}; </P> <p> void usemallocfree (void) <br/>{< br/> OBJ * A = (OBJ *) malloc (sizeof (OBJ )); // apply for dynamic memory <br/> A-> initialize (); // initialize <br/> //... <br/> A-> destroy (); // clear the job <br/> free (); // release memory <br/>}</P> <p> void usenewdelete (void) <br/>{< br/> OBJ * A = new OBJ; // apply for dynamic memory and initialize it <br/> //... <br/> Delete A; // clear and release the memory <br/>}

In this example, how to use malloc/free and new/Delete to implement the object's dynamic memory management class OBJ function initialize to simulate the constructor function, and destroy to simulate the destructor function. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. The usenewdelete function is much simpler. 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. Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? 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.

 

Ii. New Delete actually calls the malloc and free functions in implementation.

 

3. In addition to memory allocation, new operator also calls constructors. The malloc function is only responsible for allocating memory.

 
New one-dimensional array:

Char * arr;
Int Len; // dynamically determine the length value

Arr = new char [Len]; // Dynamic Allocation, you can also use malloc
...
Delete [] arr; // do not forget to release

New multi-dimensional array:

The correct method is to declare an n-dimensional array. Each unit is a pointer to a char, and then each unit is allocated separately.

Memory. The Code is as follows:

Char ** array = new char * [N];
For (INT I = 0; I <n; I ++)

Array [I] = new char [m];

 

Note: pay special attention to the above Code when releasing the allocated memory. Because this is "deep memory allocation ",

To release the memory pointed to by the pointer in each unit. The code for releasing memory is as follows:

For (I = 0; I <n; I ++)

Delete [] array [I];
Delete [] array;

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.