In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today I will show you how to make a comparative analysis of malloc/free and new/delete. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
Similarities: both can be used to request dynamic memory and free memory
Differences:
(1) the operating objects are different.
Malloc and free are the standard library functions of Candlespace C language, and new/delete is the operator of C++. For objects that are not internal data classes, maloc/free alone can not meet the requirements of dynamic objects. The constructor is automatically executed when the object is created, and the destructor is automatically executed before the object dies. Because malloc/free is a library function, not an operator, it is outside the compiler's control, and the task of executing constructors and destructors cannot be imposed on malloc/free.
(2) the usage is also different.
The prototype of the function malloc is as follows:
Void * malloc (size_t size)
Apply for a piece of memory of integer type length with malloc. The procedure is as follows:
Int * p = (int *) malloc (sizeof (int) * length)
We should focus on two elements: "type conversion" and "sizeof".
1. The type of the return value of malloc is void *, so when calling malloc, you need to explicitly convert void * to the desired pointer type.
2. The malloc function itself does not recognize the type of memory to be applied for, it only cares about the total number of bytes of memory.
The prototype of the function free is as follows:
Void free (void * memblock)
Why is the free function not as complex as the malloc function? This is because the type of pointer p and the amount of memory it refers to are known in advance, and the statement free (p) releases memory correctly. If p is a NULL pointer, free will not have a problem with p no matter how many times it is manipulated. If p is not a NULL pointer, then free operating on p twice in a row will result in a program running error.
The main points of using new/delete:
The operator new is much easier to use than the function malloc, for example:
Int * p1 = (int *) malloc (sizeof (int) * length); int * p2 = new int [length]
This is because new has built-in features such as sizeof, type conversion, and type safety checking. For objects of non-internal data types, new completes initialization while creating dynamic objects. If an object has more than one constructor, then new statements can also take many forms.
If you use new to create an array of objects, you can only use the object's no-argument constructor.
Obj * objects = new Obj [100]; / / create 100 dynamic objects cannot be written as Obj * objects = new Obj [100] (1); / / assign an initial value of 1 while creating 100 dynamic objects
When releasing an array of objects with delete, be careful not to lose the symbol'[]'.
For example:
Delete [] objects; / / correct usage delete objects; / / wrong usage
The latter is equivalent to delete objects [0], leaving out 99 other objects. (objects is the array first address)
Let's talk about the difference between the two:
1. New automatically calculates the space that needs to be allocated, while malloc needs to calculate the number of bytes manually
2. New is type-safe, but malloc is not, for example:
Int* p = new float [2]; / / errors are pointed out at compile time int* p = malloc (2*sizeof (float)); / / errors cannot be pointed out at compile time
New operator consists of two steps, operator new and construct
3. Operator new corresponds to malloc, but operator new can be overloaded, memory allocation policy can be customized, memory allocation is not even made, or even allocated to non-memory devices. And malloc can't do anything about it.
4. New will call constructor, but malloc cannot; delete will call destructor, but free cannot.
5. Malloc/free needs library file support, but new/delete does not.
Look at the following code:
Class Obj {public: Obj () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.