In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the difficulties in the C++ standard library", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn what are the difficulties in the C++ standard library.
In the C++ standard library, the restrictions on the value of default variables are very vague. Based on this, many compilers allow developers to include default variable values in function declarations, pointers and references to functions, pointers to member functions, and typedef declarations.
An exception to a situation that requires better memory control is a program that needs to run uninterrupted for a long time with limited resources. Real-time systems often need to obtain guaranteed and predictable memory with minimal cost. This leads to the need for better memory control.
In general, these programs avoid dynamic memory allocation and use special-purpose memory allocators to manage limited resources. In addition, there are situations where objects need to be placed in a specified memory location due to hardware or system requirements. This also requires custom memory management (achieved by overloading new).
Global new operator functions are used when assigning objects of type built-in, class objects that do not contain user-defined new operator functions, and arrays of any type. When the C++ standard library is customized in the class and the memory of the object of this class is allocated, the new operator of the class is called. As follows:
# include # include class Blanks {public: Blanks () {} void * operator new (size_t stAllocateBlock, char chInit);}; void * Blanks::operator new (size_t stAllocateBlock, char chInit) {void * pvTemp = malloc (stAllocateBlock); if (pvTemp! = 0) memset (pvTemp, chInit, stAllocateBlock); return pvTemp;} int main () {Blanks * a5 = new (0xa5) Blanks;// create object Blanks and pilot it to 0xa5 return a5! = 0;}
The new operator can be overloaded, but not delete. Because when it's time to release, all we can get is a pointer. And the pointer may not be the original object type pointer (it is possible that the type was converted). In fact, when you use new to get a pointer to a piece of memory, there is an indicator (indicator) in front of that piece of memory to record the actual amount of memory allocated. When you call delete, you can know how much memory needs to be freed. Array release (Deallocating Arrays):
Void f () {X* p1 = new X [10]; / /. Delete [] X;}
Why not use delete [10] X; to free memory? Bjarne Stroustrup says this can easily lead to errors, and puts the task of recording the number of elements in the implementation of delete.
In addition, the C++ standard library provides an intelligent pointer auto_ptr, which can help us prevent "resource leakage when an exception is thrown". But the disadvantage is that the smart pointer cannot point to the array because its internal memory is freed through delete rather than delete [].
Therefore, you can only use it to point to a single object. The template part is the difficult part of C++, and it is also the charm of C++.
At this point, I believe that you have a deeper understanding of the "C++ standard library difficulties", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.