std::vector _M_start 和 _M_finish 是一样的

std::vector _M_start and _M_finish are the same

我正在 GDB 中调试 std::vector,但我无法判断向量认为其当前大小是多少。向量声明为 std::vector<custom_struct_t *> myVec;.

(gdb) p myVec.size()
Cannot evaluate function -- may be inlined

检查向量:

(gdb) p myVec
[...]
    _M_impl = {
      [...]
      _M_start = 0x8052a0500, 
      _M_finish = 0x8052a0500, 
      _M_end_of_storage = 0x8052a0600
    }
  }, <No data fields>}

在这里我看到 _M_start_M_finish 是相同的值。这表示 零长度 向量还是 单长度 向量?

对于 libstdc++ (gcc) 实现,这表示大小为 0 的向量,如 std::vector::size() 函数所示:

size_type
size() const _GLIBCXX_NOEXCEPT
{ return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }

例如,可以在 GCC online documentation.

中找到此源代码