Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between malloc/free and new/delete in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the difference between malloc/free and new/delete in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem description

In C++, it is generally possible to use malloc/free and new/delete to dynamically request and free memory.

Malloc/free

The free () function is used to free the memory allocated by the malloc () function and is called in pairs with malloc ().

The argument of the free () function is of pointer type. When releasing, you need to determine whether the pointer is NULL or not.

The free () function releases memory and does not call the destructor of the pointer object.

Free () and malloc () are operations on memory space

New/delete

Delete is used to free memory allocated by new, which is called in pairs with new

Delete does not need to judge whether the following object is NULL or not

Delete not only frees memory, but also calls the object's destructor.

Delete and new are operations on objects and are operators

problem

If you forget to call delete or because it is not called in other ways, the memory allocated in the heap will not be automatically reclaimed, resulting in a memory leak; now you can generally choose to use smart pointers, which are now supported by the cantileaked 11 standard.

This is the end of the content of "what's the difference between malloc/free and new/delete in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report