abi::__cxa_demangle 不能重用自己返回的内存
abi::__cxa_demangle can't reuse memory returned by itself
我尝试使用abi::__cxa_demangle
来要求用户定义的类型:
#include <iostream>
#include <mutex>
#include <memory>
#include <string>
#include <typeinfo>
#include <cassert>
#include <cstdlib>
#include <cxxabi.h>
namespace
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
std::mutex m;
std::unique_ptr< char, decltype(std::free) & > demangled_name{nullptr, std::free};
#pragma clang diagnostic pop
}
inline
std::string
get_demangled_name(char const * const symbol) noexcept
{
if (!symbol) {
return "<null>";
}
std::lock_guard< std::mutex > lock(m);
int status = -4;
demangled_name.reset(abi::__cxa_demangle(symbol, demangled_name.release(), nullptr, &status));
return ((status == 0) ? demangled_name.get() : symbol);
}
template< typename ...types >
void
f(std::size_t const i)
{
using F = void (*)();
assert(i < sizeof...(types));
static F const a[sizeof...(types)] = {static_cast< F >([] () { std::cout << get_demangled_name(typeid(types).name()) << std::endl; })...};
return a[i]();
};
struct A {};
struct B {};
struct X {};
struct Y {};
struct Z {};
int
main()
{
for (std::size_t i = 0; i < 5; ++i) {
f< A, B, X, Y, Z >(i);
}
return EXIT_SUCCESS;
}
但是abi::__cxa_demangle
返回status
"-3:其中一个参数无效。"每隔一秒
在第一次调用时(对于A
),智能指针包含nullptr
, abi::__cxa_demangle
返回零status
"0:要求操作成功"。但是文档说:
output_buffer:使用malloc分配的内存区域,长度为*字节,其中存储了被请求的名称。如果output_buffer不够长,则使用realloc对其进行扩展。output_buffer可以改为NULL;在这种情况下,被请求的名称被放置在由malloc分配的内存区域中。
因此,我得出结论,该函数不能重用自己一致分配的内存。这是bug还是我对文档的误解?
你误解了文档:
output_buffer:用malloc分配的*字节长度的内存区域
你正在传递一个由malloc
分配的内存区域,但是length
是空的,所以*length
没有定义。
为了知道它是否可以重用内存,它需要知道块有多大,所以你需要将长度作为第三个参数传入。
在GCC中的实现(实际上在自由支持库中)做:
if (output_buffer != NULL && length == NULL)
{
if (status != NULL)
*status = -3;
return NULL;
}
所以如果你传递一个非空的output_buffer
指针,你也必须传递一个非空的length
指针。
因为你不知道分配的块有多大,所以你能做的最好的事情就是使用strlen(demangled_name.get())+1
来找到确定分配的最小长度
最好是保留一个size_t
全局变量来存储之前的大小,并将其传递进去。你应该把这些都包装在一个类中,而不仅仅是一堆全局变量。
在gcc下使用此代码。它是可靠的。
#include <cxxabi.h>
#include <memory>
#include <string>
#include <cassert>
template <typename T>
std::string demangle(T& e)
{
int status;
std::unique_ptr<char> realname;
const std::type_info &ti = typeid(e);
realname.reset(abi::__cxa_demangle(ti.name(), 0, 0, &status));
assert(status == 0);
return std::string(realname.get());
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++ lambda 返回自己
- 返回自己的类型的模板类方法的正确签名
- 函数可以将指针返回其自己的类型
- 我创建了自己的智能指针类,我用这个智能指针指向一个类实例,如何正确返回智能指针
- 如何在 c++ 中返回我自己的结构
- LIBSVM SVM 在如何使用我自己(不带"svmpredict")与从"svmtrain"返回的参数
- C++ std::函数,返回它自己的类型(再次递归类型)
- 从 MapViewOfFile() 的返回值构造自己的对象
- 使clang格式允许函数在自己的行上返回类型
- 如何从Oracle的过程中返回我自己的结构的数组/记录集?
- 对象在返回之前破坏自己?c++
- abi::__cxa_demangle 不能重用自己返回的内存
- 为什么采用向量的方法不能采用自己输出的返回向量?
- 递归typedef函数定义:std::函数返回自己的类型
- 返回的结构调用它自己的析构函数,用于销毁分配的对象