Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ abi::\uuucxa\udemangle——为什么缓冲区需要'malloc'-ed?_C++_Api_Abi_Demangler - Fatal编程技术网

C++ abi::\uuucxa\udemangle——为什么缓冲区需要'malloc'-ed?

C++ abi::\uuucxa\udemangle——为什么缓冲区需要'malloc'-ed?,c++,api,abi,demangler,C++,Api,Abi,Demangler,abi::\uuucxa\u demangle(例如)的文档指定第二个参数char*output\u buffer,需要malloc-ed 这意味着不允许在堆栈上分配以下字符缓冲区 enum {N = 256}; char output_buffer[N]; size_t output_length = 0; int status = -4; char * const result = std::__cxa_demangle(mangled_name,

abi::\uuucxa\u demangle
(例如)的文档指定第二个参数
char*output\u buffer
,需要
malloc
-ed

这意味着不允许在堆栈上分配以下字符缓冲区

  enum {N = 256};
  char output_buffer[N];
  size_t output_length = 0;
  int status = -4;
  char * const result = std::__cxa_demangle(mangled_name,
                        output_buffer, &output_length, &status);
两个问题:

  • 为什么堆栈上不允许使用
    输出缓冲区

  • 当输出缓冲区已经传递时,为什么返回不同的指针

  • 受示例的影响,我会想象一个API,如下所示

    // Demangle the symbol in 'mangled_name' and store the output
    // in 'output_buffer' where 'output_buffer' is a caller supplied
    // buffer of length 'output_buffer_length'. The API returns the 
    // number of bytes written to 'output_buffer' which is not
    // greater than 'output_buffer_length'; if it is
    // equal to 'output_buffer_length', then output may have been
    // truncated.
    size_t mydemangle(char const * const mangled_name,
                      char * output_buffer,
                      size_t const output_buffer_length);
    

    1) 为什么不允许在堆栈上使用输出缓冲区

    从您提供的链接<代码>如果输出缓冲区不够长,则使用realloc扩展它。无法调整堆栈上的数据大小,因为堆栈帧的大小通常是固定的(特殊情况是
    alloca

    2) 当输出缓冲区已经传递时,为什么返回不同的指针

    当使用realloc时,没有理由认为您将返回相同的指针。例如,如果该位置没有足够的连续可用内存,操作系统将需要在其他位置分配内存

    如果我不得不猜测API为什么是这样设计的,那就是通常认为不在函数中分配内存然后返回对该内存的引用是一种良好的做法。相反,让调用者同时负责分配和解除分配。这有助于避免意外的内存泄漏,并允许API用户设计自己的内存分配方案。我很欣赏这些东西,因为它允许用户利用自己的内存管理方案来避免内存碎片之类的事情。虽然
    realloc
    的潜在用途有点把这个想法搞砸了,但是您可以通过为输出参数分配足够大的块来解决这个问题,这样就永远不会调用
    realloc


  • 为什么不允许在堆栈上使用输出缓冲区
  • 当输出缓冲区已经传递时,为什么返回不同的指针
  • 因为C++类名称可以任意长。 试试这个:

    #include <iostream>
    #include <cxxabi.h>
    #include <utility>
    
    using foo = std::make_index_sequence<10000>;
    
    int main()
    {
        size_t buff_size = 128;
        auto buff = reinterpret_cast<char*>(std::malloc(buff_size));
        std::cout << "buffer before: " << static_cast<void*>(buff) << std::endl;
        int stat = 0;
        buff = abi::__cxa_demangle(typeid(foo).name(), buff, &buff_size, &stat);
        std::cout << "buffer after: " << static_cast<void*>(buff) << std::endl;
        std::cout << "class name: " << buff << std::endl;
        std::free(buff);
    }
    
    #包括
    #包括
    #包括
    使用foo=std::生成索引序列;
    int main()
    {
    大小\u t buff\u size=128;
    自动增益=重新解释投影(标准::malloc(增益大小));
    
    std::cout如果某些东西需要malloc'd,通常是因为其他东西将调用free或realloc。“为什么不允许堆栈上的输出缓冲区?”——“如果输出缓冲区不够长,则使用realloc扩展它。”.正是,从您提供的链接中。
    如果输出缓冲区不够长,则使用realloc扩展它
    @DanielKamilKozar答案部分如下。谢谢大家。很抱歉,我没有仔细阅读,忽略了realloc
    部分。
    buffer before: 0x7f813d402850
    buffer after: 0x7f813e000000
    class name: std::__1::integer_sequence<unsigned long, 0ul, 1ul, 2ul, 3ul, 4ul, 5ul, 6ul, 7ul, 8ul, 9ul, 10ul, 11ul, 12ul, 13ul, 14ul, 15ul, 16ul, 17ul, ... and so on...