Differences between Malloc and new

Source: Internet
Author: User

1. malloc and free are standard library functions of C ++/C, and new/delete are operators of C ++. They can be used to apply for dynamic memory and release memory.

2. For non-Internal data objects, the use of 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.

3. 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.

4. C ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory.

New is an operator. It has the same status as what "+", "-", "=.
Malloc is a memory allocation function for you to call.

New is a reserved word and does not need to be supported by header files.
Malloc requires support for the header file library function.

New creates an object,
Malloc allocates a piece of memory.

What is free () released?In short:
New is an operator that can be reloaded.
Malloc is a function that can overwrite
New initializes the object and calls the object constructor. The corresponding delete calls the corresponding destructor.
Malloc only allocates memory, free only recycles memory

This is a simple question. In fact, I want to echo the question of the second largest part! Haha! Free () releases the memory pointed to by the pointer! Note! Memory is released, not a pointer! This is very important! A pointer is a variable and is destroyed only when the program ends. After the memory space is released, the pointer to this space still exists! But now the Pointer Points to the content of the garbage, is undefined, so said garbage. Therefore, as I have mentioned earlier, after the memory is released, the pointer is directed to NULL to prevent the pointer from being accidentally referenced. Very important.

You can use a new object as a common object to access it using a member function instead of directly accessing its address space.
Malloc is allocated with a memory area, which can be accessed with a pointer and moved inside.

Malloc/free and new/delete operations are often encountered during C/C ++ programming and development, the main function is to dynamically apply for and release the memory while the program is running, so as to perform operations on the memory. However, these two operations are different. They cannot be used in combination: they cannot free up the new memory or delete the memory space of malloc. Although sometimes you can delete the memory from malloc or free the memory from new, it usually causes unpredictable errors to the program, I believe this is not what programmers want to see. A good habit is to strictly pair and use: use free to release the memory space of malloc and use delete to release the memory space of new.

The differences between the two operations:

1. malloc/free is the method (function) in C/C ++, and new/delete is the operator in C ++.

2. malloc applies for heap memory, while new applies for the free store memory.

3. Before using free, make sure that the free pointer is! If it is NULL, delete is not required.

4. The free memory is a memory space pointed to by the pointer, which should be empty. The deleted memory does exist.

Data or object.

5. The following example shows the differences:

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; // refer to Clause 5: Why should 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 all with one mind

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.