LLDB:打印一个shared_ptr引用的向量

LLDB: printing a vector referenced by a shared_ptr

本文关键字:shared ptr 引用 向量 一个 打印 LLDB      更新时间:2023-10-16

在我的代码中有这样的东西:

shared_ptr<vector<unsigned int>> f = 
    make_shared<vector<unsigned int>>();

我如何才能漂亮地打印矢量,因为我只能使用访问shared_ptr对象

 frame variable f

 frame variable f.__ptr_->size()
 call to a function 'std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::size() const' that is not present in the target

得到这个错误吗?

给定以下代码片段:

#include <vector>
#include <memory>
using namespace std;
int main()
{
    shared_ptr<vector<unsigned int> > f = 
        make_shared<vector<unsigned int> >();
    f->push_back(1);
    f->push_back(1);
    f->push_back(1);
    return 0;
}

LLDB只是为我工作:

(lldb) fr var
(std::__1::shared_ptr<std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > >) f = 0x00000001001038c8 size=3 (strong=1 weak=1) {
  __ptr_ = 0x00000001001038c8 size=3
}

更好的是,如果我扩展__ptr_:

(lldb) fr var --ptr-depth=2 
(std::__1::shared_ptr<std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > >) f = 0x00000001001038c8 size=3 (strong=1 weak=1) {   __ptr_ = 0x00000001001038c8 size=3 {
    [0] = 1
    [1] = 1
    [2] = 1   } }