In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the C++ New example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
There are three typical ways to use New in C++ today, corresponding to the following three formats.
1.new: a normal new operation. Once memory allocation fails, an exception is thrown directly. You need to use catch to handle this exception information.
2. New (nothrow): a new operation that does not throw an exception. If memory allocation fails, an exception is prohibited. Instead, an NULL is returned. You can handle this exception by determining whether the pointer is NULL.
3. Placement new: directly reuse the new operation of the pointer that has been allocated memory. This operation will only call the constructor to re-initialize this part of the memory.
Examples are as follows:
The a==NULL in the function Func1 () can capture scenarios where memory allocation fails.
The function Func2 (), a==NULL does not protect the scenario where memory allocation fails, and needs to be handled with catch.
The function Func3 (), after the placement new operation, the address of b is the same as that of a, except that b initializes this part of memory again.
The running result of the program is as follows:
From the print, we can see that the addresses pointed to by an and b are the same, and the a5 becomes 8, as shown in the following figure:
Ash was made on December 19, 2018.
Thank you for reading this article carefully. I hope the article "sample Analysis of New in C++" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
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.