本文介绍了替换标准C ++分配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个更强大的分配器替换标准分配器(C ++标准只需要对vector :: resize进行溢出检查)。

I want to replace the standard allocator with a more robust allocator (the C++ standard only requires an overflow check on vector::resize). The various C++ allocators supplied with many libraries fall flat on their face when fed negative self tests.

我可以访问一个更健壮的分配器。 ESAPI的分配器不仅检查溢出,它还有调试工具来帮助发现错误。 。

I have access to a more robust allocator. ESAPI's allocator not only checks for overflow, it also has debug instrumentation to help find mistakes. http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h.

替换在程序中使用的C ++分配器没有太多的努力?

Is there a standard way to replace the C++ allocator used in a program without too much effort? I also want to ensure its replaced in library code, which I may not have access to source code.

推荐答案

与<$ c不同,我不能访问源代码。 $ c> malloc 这是一个可以被具有相同签名的另一个函数替换的库函数, std :: allocator 是一个类模板,模板代码根据需要实例化,并内联到使用它的代码中。一些标准库代码已经被编译到库的目标文件中,并且将包含不能被替换的实例化的 std :: allocator 代码。所以唯一的方法是如果标准库提供了一些非标准的方法来替换它的 std :: allocator 。幸运的是,GCC的libstdc ++允许你这样做,当你配置和构建GCC时,允许你选择 std :: allocator 的实现,有几个

Unlike malloc which is a library function that can be replaced by another function with the same signature, std::allocator is a class template and template code is instantiated as needed and inlined into code that uses it. Some standard library code will have already been compiled into the library's object files and will contain instantiated std::allocator code which can't be replaced. So the only way is if the standard library provides some non-standard way to replace its std::allocator. Luckily, GCC's libstdc++ allows you to do just that, allowing you to select the implementation used for std::allocator when GCC is configured and built, with a few different choices

将ESAPI分配器添加到GCC源作为选项之一不是太多的工作,然后重建GCC以使用该分配器作为 std :: allocator 提供它的实现。您可能需要调整ESAPI分配器代码一点,也许可以改变libstdc ++ configure 脚本,以允许您说 - enable-libstdcxx-allocator = esapi

It wouldn't be too much work to add the ESAPI allocator to the GCC sources as one of the options, then rebuild GCC to use that allocator as the base class of std::allocator providing its implementation. You might need to tweak the ESAPI allocator code a bit, and maybe alter the libstdc++ configure script to allow you to say --enable-libstdcxx-allocator=esapi

这篇关于替换标准C ++分配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:10