简体   繁体   中英

stl::vector fails to allocate memory 'randomly'

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM