簡體   English   中英

std :: string如何使用-fwhole-program在GCC中分配內存?

[英]How does std::string allocate memory in GCC with -fwhole-program?

更新:以下問題似乎取決於-fwhole-program選項。

我一直在研究內存分配問題,但遇到一個小謎團:在GCC(4.6)中,當我使用-fwhole-program [/]編譯時, std::string如何分配其內存[ edit ]?

擁有以下測試程序:

#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.";
}

當我使用任何其他動態容器(使用std::allocator<T> )時,分配器使用::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() requests 24 bytes, allocated at 0x8d53028我輸出了new() requests 24 bytes, allocated at 0x8d53028等)

查看帶有/不-fwhole-programg++ ... -S的輸出,似乎在使用fwhole-program時根本沒有發出整個自定義new / delete運算符。

我開始懷疑我們在這里正在尋找錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM