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?
- ClassOBJ
- {
- Public:
- OBJ (Void) {Cout <"initialization" <Endl ;}
- ~ OBJ (Void) {Cout <"destroy" <Endl ;}
- VoidInitialize (Void) {Cout <"initialization" <Endl ;}
- VoidDestroy (Void) {Cout <"destroy" <Endl ;}
- };
- VoidUsemallocfree (Void)
- {
- OBJ * A = (OBJ *) malloc (Sizeof(OBJ); // apply for dynamic memory
- A-> initialize (); // Initialization
- //...
- A-> destroy (); // clear the job
- Free (a); // releases the memory.
- }
- VoidUsenewdelete (Void)
- {
- OBJ * A =NewOBJ; // apply for dynamic memory and initialize
- //...
- DeleteA; // clear and release the memory
- }
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?
- ClassOBJ
- {
- Public:
- OBJ (Void) {Cout <"initialization" <Endl ;}
- ~ OBJ (Void) {Cout <"destroy" <Endl ;}
- VoidInitialize (Void) {Cout <"initialization" <Endl ;}
- VoidDestroy (Void) {Cout <"destroy" <Endl ;}
- };
- VoidUsemallocfree (Void)
- {
- OBJ * A = (OBJ *) malloc (Sizeof(OBJ); // apply for dynamic memory
- A-> initialize (); // Initialization
- //...
- A-> destroy (); // clear the job
- Free (a); // releases the memory.
- }
- VoidUsenewdelete (Void)
- {
- OBJ * A =NewOBJ; // apply for dynamic memory and initialize
- //...
- DeleteA; // clear and release the memory
- }
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;