stl::vector 无法“随机”分配内存;
我遇到了一个奇怪的问题,很好奇是否有其他人看到过这种现象。我正在使用随机算法处理图表,因此每次运行的种子都是不同的。
unsigned int sseed = time(0);
srand(sseed);
尽管我的代码使用了相当多的内存,但它不需要使用所有可用内存。当我运行我的代码时,90% 以上的时间它都能顺利运行。然而,对于特定的种子值,我遇到了内存问题。当我调整向量 CC 的大小时会发生这种情况:
vector<double> tmp_CC,CC;
tmp_CC.resize(SAMPLE_SIZE+1,0.0);
CC.resize(numberOfNodes+1,0.0); // line 1480
这是调试器的输出。
Program received signal SIGSEGV, Segmentation fault.
0x481d4cdb in malloc_pages (size=86016) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:543
543 /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c: No such file or directory.
in /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c
Current language: auto; currently c
(gdb) backtrace
#0 0x481d4cdb in malloc_pages (size=86016) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:543
#1 0x481d522f in imalloc (size=82504) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:738
#2 0x481d5d7a in pubrealloc (ptr=0x0, size=82504, func=0x4825ba17 " in malloc():")
at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:1126
#3 0x481d5e6b in malloc (size=82504) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:1150
#4 0x48126e0d in operator new () from /usr/local/lib/gcc/i386-unknown-freebsd6.1/3.4.6/libstdc++.so.6
#5 0x08053b4f in __gnu_cxx::new_allocator<double>::allocate (this=0xbfbfe7e0, __n=10313) at new_allocator.h:81
#6 0x0805229a in std::_Vector_base<double, std::allocator<double> >::_M_allocate (this=0xbfbfe7e0, __n=10313) at stl_vector.h:113
#7 0x08052566 in std::vector<double, std::allocator<double> >::_M_fill_insert (this=0xbfbfe7e0, __position={_M_current = 0x0},
__n=10313, __x=@0xbfbfe740) at vector.tcc:308
#8 0x080509f4 in std::vector<double, std::allocator<double> >::insert (this=0xbfbfe7e0, __position={_M_current = 0x0}, __n=10313,
__x=@0xbfbfe740) at stl_vector.h:612
#9 0x0804fa40 in std::vector<double, std::allocator<double> >::resize (this=0xbfbfe7e0, __new_size=10313, __x=@0xbfbfe740)
at stl_vector.h:398
#10 0x0804da71 in calc_closeness_L () at SSDE.h:1480
#11 0x0804e8fd in main (argv=7, argc=0xbfbfeaec) at closeness.cpp:113
随机化会影响我从哪些节点开始执行 dijkstra 算法,但不会直接(我认为也不会间接)影响分配的内存量。
有人见过这个“随机”分段错误问题吗?或者在代码中检查可能存在的错误?
谢谢!
I have what seems to be an odd problem and am curious if anyone else has seen this phenomenon. I am processing a graph using a random algorithm, so the seed is different for each run.
unsigned int sseed = time(0);
srand(sseed);
Although my code uses a fair amount of memory, it doesn't need to use all available memory. When I run my code, 90+% of the time it works without a hitch. However, with particular seed values, I run into memory problems. It occurs when I resize the vector CC:
vector<double> tmp_CC,CC;
tmp_CC.resize(SAMPLE_SIZE+1,0.0);
CC.resize(numberOfNodes+1,0.0); // line 1480
and I this is the output from the debugger.
Program received signal SIGSEGV, Segmentation fault.
0x481d4cdb in malloc_pages (size=86016) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:543
543 /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c: No such file or directory.
in /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c
Current language: auto; currently c
(gdb) backtrace
#0 0x481d4cdb in malloc_pages (size=86016) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:543
#1 0x481d522f in imalloc (size=82504) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:738
#2 0x481d5d7a in pubrealloc (ptr=0x0, size=82504, func=0x4825ba17 " in malloc():")
at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:1126
#3 0x481d5e6b in malloc (size=82504) at /.amd/distserv/share0/FreeBSD-6.3/src/lib/libc/stdlib/malloc.c:1150
#4 0x48126e0d in operator new () from /usr/local/lib/gcc/i386-unknown-freebsd6.1/3.4.6/libstdc++.so.6
#5 0x08053b4f in __gnu_cxx::new_allocator<double>::allocate (this=0xbfbfe7e0, __n=10313) at new_allocator.h:81
#6 0x0805229a in std::_Vector_base<double, std::allocator<double> >::_M_allocate (this=0xbfbfe7e0, __n=10313) at stl_vector.h:113
#7 0x08052566 in std::vector<double, std::allocator<double> >::_M_fill_insert (this=0xbfbfe7e0, __position={_M_current = 0x0},
__n=10313, __x=@0xbfbfe740) at vector.tcc:308
#8 0x080509f4 in std::vector<double, std::allocator<double> >::insert (this=0xbfbfe7e0, __position={_M_current = 0x0}, __n=10313,
__x=@0xbfbfe740) at stl_vector.h:612
#9 0x0804fa40 in std::vector<double, std::allocator<double> >::resize (this=0xbfbfe7e0, __new_size=10313, __x=@0xbfbfe740)
at stl_vector.h:398
#10 0x0804da71 in calc_closeness_L () at SSDE.h:1480
#11 0x0804e8fd in main (argv=7, argc=0xbfbfeaec) at closeness.cpp:113
The randomization affects which nodes I start at to perform dijkstra's algorithm, but doesn't directly (nor, do I think, indirectly) affects the amount of memory allocated.
Anyone ever seen this 'random' segmentation fault problem? Or possible bugs to check for in the code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当内存分配期间偶尔出现神秘的段错误时,这通常意味着程序的某些其他部分正在写入已释放的内存、注销数组的末尾或以其他方式破坏 malloc 的内部结构。此类问题可能非常难以调试。
Oli Charlesworth 建议您在 valgrind(一种检测不良内存访问的工具)下运行程序。这是个好建议,你应该尝试一下。
When you have an occasional, mysterious segfault during memory allocation, that usually means that some other part of the program is writing into freed memory, writing off the end of an array, or otherwise corrupting malloc's internal structures. These sorts of problems can be very difficult to debug.
Oli Charlesworth's suggests that you run your program under
valgrind
(a tool that detects bad memory accesses). This is good advice, and you should try it.