abi::__cxa_demangle
(例如https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html )的文档指定第二个参数 ,char * output_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);
两个问题:
为什么
output_buffer
不允许在堆栈上?为什么在已经传递了输出缓冲区时返回了不同的指针?
受backtrace()示例的影响,我会想象一个类似以下的 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);