创建集合的向量

发布于 2024-11-30 08:23:26 字数 2323 浏览 2 评论 0原文

在我的代码中,我定义了一个向量:

vector<vector<vector<vector<shared_ptr<foo> > > > > fooBoxes;

我正在使用以下方式初始化向量:

int BOX_NUM = 12 //this is actually defined elsewhere

fooBoxes.resize(BOX_NUM);
for (int i = 0; i<BOX_NUM; i++){
  fooBoxes[i].resize(BOX_NUM);
  for (int j = 0; j < BOX_NUM; j++){
    fooBoxes[i][j].resize(BOX_NUM);
    for (int k = 0; k < BOX_NUM; k++){
      fooBoxes[i][j][k].resize(0);
    }
  }
}

我怀疑向量的使用导致了我的分段错误,我想将 fooBoxes 替换为:

vector<vector<vector<set<shared_ptr<foo> > > > > fooBoxes

我在中做什么for 循环?只需删除 resize(0) 部分?

编辑:
这是崩溃中 valgrind 的输出:

==2258== Invalid read of size 8
==2258==    at 0x439237: trans(int) (stl_iterator.h:704)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  Address 0x7932420 is 8 bytes after a block of size 24 free'd
==2258==    at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==2258==    by 0x405636: vec::~vec() (valarray_array.h:71)
==2258==    by 0x437D66: trans(int) (transFile.cpp:64)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==
==2258== Invalid read of size 8
==2258==    at 0x439240: trans(int) (stl_vector.h:604)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  Address 0x111 is not stack'd, malloc'd or (recently) free'd
==2258==
==2258==
==2258== Process terminating with default action of signal 11 (SIGSEGV)
==2258==  Access not within mapped region at address 0x111
==2258==    at 0x439240: trans(int) (stl_vector.h:604)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  If you believe this happened as a result of a stack
==2258==  overflow in your program's main thread (unlikely but
==2258==  possible), you can try to increase the size of the
==2258==  main thread stack using the --main-stacksize= flag.
==2258==  The main thread stack size used in this run was 10485760.

我相信问题是当我尝试删除/将珠子放入向量中时不够小心,这就是我想移动到 set 的原因。

In my code I have a vector defined:

vector<vector<vector<vector<shared_ptr<foo> > > > > fooBoxes;

I am initializing the vector using:

int BOX_NUM = 12 //this is actually defined elsewhere

fooBoxes.resize(BOX_NUM);
for (int i = 0; i<BOX_NUM; i++){
  fooBoxes[i].resize(BOX_NUM);
  for (int j = 0; j < BOX_NUM; j++){
    fooBoxes[i][j].resize(BOX_NUM);
    for (int k = 0; k < BOX_NUM; k++){
      fooBoxes[i][j][k].resize(0);
    }
  }
}

I suspect that the use of vector is causing me a segmentation fault and I want to replace fooBoxes to be:

vector<vector<vector<set<shared_ptr<foo> > > > > fooBoxes

what do I do in the for loops? simply remove the resize(0) part?

Edit:
This is the output of valgrind in the crash:

==2258== Invalid read of size 8
==2258==    at 0x439237: trans(int) (stl_iterator.h:704)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  Address 0x7932420 is 8 bytes after a block of size 24 free'd
==2258==    at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==2258==    by 0x405636: vec::~vec() (valarray_array.h:71)
==2258==    by 0x437D66: trans(int) (transFile.cpp:64)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==
==2258== Invalid read of size 8
==2258==    at 0x439240: trans(int) (stl_vector.h:604)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  Address 0x111 is not stack'd, malloc'd or (recently) free'd
==2258==
==2258==
==2258== Process terminating with default action of signal 11 (SIGSEGV)
==2258==  Access not within mapped region at address 0x111
==2258==    at 0x439240: trans(int) (stl_vector.h:604)
==2258==    by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258==    by 0x402767: main (main.cpp:14)
==2258==  If you believe this happened as a result of a stack
==2258==  overflow in your program's main thread (unlikely but
==2258==  possible), you can try to increase the size of the
==2258==  main thread stack using the --main-stacksize= flag.
==2258==  The main thread stack size used in this run was 10485760.

I believe that the problem is that I'm not careful enough when I try to delete/put a bead into a vector and this is why I want to move to set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2024-12-07 08:23:26

这取决于您想要实现的目标。如果删除 fooBoxes[i][j][k].resize(0);,您的代码肯定会编译,因为 std::set 不支持此函数。

That depends on what you want to achieve. Your code will most definitely compile if you remove fooBoxes[i][j][k].resize(0); since the std::set does not support this function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文