std :: string如何使用-fwhole程序在GCC中分配内存? [英] How does std::string allocate memory in GCC with -fwhole-program?

查看:179
本文介绍了std :: string如何使用-fwhole程序在GCC中分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:以下问题似乎取决于 -fwhole-program 选项。



我一直在玩内存分配,我遇到了一个小谜:在GCC(4.6), std :: string 如何分配它当我使用 -fwhole-program [/]编译时


$ b >有以下测试程序:

  #include< new> 
#include< string>
#include< iostream>
#include< cstdlib>

void * operator new(std :: size_t n)throw(std :: bad_alloc)
{
void * const p = std :: malloc

if(p == NULL)throw std :: bad_alloc();

std :: cerr<< new()requests< n<< 字节,分配在< p < .\\\
;

return p;
}

void operator delete(void * p)noexcept
{
std :: cerr< delete()at<< p < .\\\
;
std :: free(p);
}

int main()
{
std :: string s =Hello world。当我使用任何其他动态容器(它使用


),分配器使用 :: operator new ,所以我很高兴地看到调试消息。但是,使用 std :: string ,我看不到任何东西。我确定动态分配发生,虽然,我可以确认与valgrind(13加上字符串长度字节分配)。我经历了几个源文件和标准,我很确定模板是 std :: basic_string< T,std :: char_traits< T>,std :: allocator< T& ,所以我很失望为什么我没有看到替换的分配函数中的消息。



任何人都可以这个难题?我该如何跟踪字符串分配?此外,任何人都可以通过一些其他编译器运行它,看看它是否产生任何输出?



(例如:如果我添加 std :: map< ; int,int> m {{0,1}}; new()请求24字节,分配在0x8d53028

解决方案

查看 g ++ ... -S 有/没有 -fwhole-program 看来,使用 fwhole-program



我开始怀疑我们在这里看到一个错误。


Update: The following problem appears to depend on the -fwhole-program option.

I've been playing around a bit with memory allocation, and I encountered a small mystery: In GCC (4.6), how does std::string allocate its memory [edit]when I compile with -fwhole-program[/]?

Have this following test program:

#include <new>
#include <string>
#include <iostream>
#include <cstdlib>

void * operator new(std::size_t n) throw(std::bad_alloc)
{
  void * const p = std::malloc(n);

  if (p == NULL) throw std::bad_alloc();

  std::cerr << "new() requests " << n << " bytes, allocated at " << p << ".\n";

  return p;
}

void operator delete(void * p) noexcept
{
  std::cerr << "delete() at " << p << ".\n";
  std::free(p);
}

int main()
{
  std::string s = "Hello world.";
}

When I use any other dynamic container (which uses std::allocator<T>), the allocator uses ::operator new, and so I see the debug messages happily. However, with std::string, I see nothing at all. I'm sure that dynamic allocation happens, though, as I can confirm with valgrind (13 plus string length bytes are allocated). I went through several source files and the standard, and I'm pretty sure that the template is std::basic_string<T, std::char_traits<T>, std::allocator<T>>, so I'm at a loss why I don't see the messages from my replaced allocation functions.

Can anyone shed any light on this conundrum? What do I do to track string allocations? Also, could anyone run this through some other compiler and see if it produces any output?

(For example: if I add std::map<int, int> m { { 0, 1 } };, I have output new() requests 24 bytes, allocated at 0x8d53028 etc.)

解决方案

Looking at the output of g++ ... -S with / without -fwhole-program it appears that the whole custom new/delete operators aren't emitted at all when using fwhole-program.

I'm starting to suspect we're looking at a bug here.

这篇关于std :: string如何使用-fwhole程序在GCC中分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆