xunwap

专注移动互联网服务

正在浏览 C++ 里的文章

从ACE文档中看到如下的内容,总结就是: 从同一个堆上分配的内存一定要释放到同一个堆中,否则系统有可能因此崩溃。
这一点在使用基于动态链接库的应用中尤为重要,因为可以出现在主程序中分配的资源,在动态库中使用,然后内存在动态库中被释放。
A surprisingly common misconception is that simply ensuring the proper matching of calls to operator new() and operator delete() (or calls to malloc() and free()) is sufficient for correct heap management. This strategy relies on the implicit assumption that there’s one universal heap per process. In practice, however, a heap is simply a memory area managed by some run-time component, such as the C or C++ run-time library. If an executing program is exposed to multiple run-time library instances, it’s likely there will be multiple heaps as well.
For example, Windows supplies multiple variants of the C/C++ run-time library, such as Debug versus Release and Multithreaded versus Single-threaded. Each of these variants maintains its own heap. Memory allocated from one heap must be released back to the same heap. Thus, correct heap management requires not only matching the proper method/function calls, but also making them through the same run-time library. It’s easy to violate these requirements when code from one subsystem or provider frees memory allocated by another.

本站原创文章,转载请注明出处。

A trait is a type that conveys information used by another class or algorithm to determine policies at compile time. A traits class [Jos99] is a useful way to collect a set of traits that should be applied in a given situation to alter another class’s behavior appropriately. Traits and traits classes are C++ policy-based class design idioms [Ale01] that are widely used throughout the C++ standard library [Aus99].

For example, the char_traits class defines the traits of a character type, such as its data type and functions to compare, search for, and assign characters of that type. The C++ standard library provides specializations of char_traits<> for char and wchar_t. These character traits then modify the behavior of common classes, such as basic_iostream<> and basic_string<>. The iostream and string classes are defined by specializing the class templates with char_traits<char>. Similarly, the wiostream and wstring classes are defined by specializing the templates with char_traits<wchar_t>.

These C++ idioms are similar in spirit to the Strategy pattern [GoF], which allows substitution of class behavioral characteristics without requiring a change to the class itself. The Strategy pattern involves a defined interface that’s commonly bound dynamically at run time using virtual methods. In contrast, the traits and traits class idioms involve substitution of a set of class members and/or methods that can be bound statically at compile time using C++ parameterized types.

本站原创文章,转载请注明出处。