2017-03-17 45 views
0

好了,这里是我的子集和算法的实现:为什么这个递归子集和算法会导致指针分配错误?

std::vector<Key> Brute::subset_sum(const std::vector<Key>& Table, Key& target, const std::vector<Key>& solution) { 
    Key sum = Key(); 

    for (std::vector<Key>::const_iterator it = solution.begin(); it != solution.end(); it++) { 
     sum += *it; 
    } 

    if (sum == target) { 
     return solution; 
    } 

    if (target < sum) { 
     return std::vector<Key>(); 
    } 
    Key key; 
    for (std::vector<Key>::const_iterator it = Table.begin(); it != Table.end(); it++) { 
     key = *it; 
     std::vector<Key> remaining; 
     for (std::vector<Key>::const_iterator jt = it; jt != Table.end(); jt++) { 
      if (jt == it) { 
       continue; 
      } 
      remaining.push_back(*it); 
     } 
     std::vector<Key> sol = solution; 
     sol.push_back(key); 
     subset_sum(remaining, target, sol); 
    } 
} 

每个按键都有一个数值和所需的比较和另外运营商已经超载。

在通过LLDB运行此,我得到下面的输出:

(lldb) 
brute(1465,0x7fffae14d3c0) malloc: *** error for object 0x7fff5fbff6b0: pointer being freed was not allocated 
*** set a breakpoint in malloc_error_break to debug 
Process 1465 stopped 
* thread #1: tid = 0x9ee7, 0x00007fffa5307d42 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT 
    frame #0: 0x00007fffa5307d42 libsystem_kernel.dylib`__pthread_kill + 10 
libsystem_kernel.dylib`__pthread_kill: 
-> 0x7fffa5307d42 <+10>: jae 0x7fffa5307d4c   ; <+20> 
    0x7fffa5307d44 <+12>: movq %rax, %rdi 
    0x7fffa5307d47 <+15>: jmp 0x7fffa5300caf   ; cerror_nocancel 
    0x7fffa5307d4c <+20>: retq 

这里的回溯:

(lldb) bt 
* thread #1: tid = 0x9ee7, 0x00007fffa5307d42 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT 
    * frame #0: 0x00007fffa5307d42 libsystem_kernel.dylib`__pthread_kill + 10 
    frame #1: 0x00007fffa53f55bf libsystem_pthread.dylib`pthread_kill + 90 
    frame #2: 0x00007fffa526d420 libsystem_c.dylib`abort + 129 
    frame #3: 0x00007fffa535cfe7 libsystem_malloc.dylib`free + 530 
    frame #4: 0x00000001000022a6 brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::__deallocate(__ptr=<unavailable>) + 822 at new:177 [opt] 
    frame #5: 0x00000001000022a1 brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::allocator<Key>::deallocate(__p=<unavailable>) at memory:1731 [opt] 
    frame #6: 0x00000001000022a1 brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::allocator_traits<std::__1::allocator<Key> >::deallocate(__p=<unavailable>) at memory:1496 [opt] 
    frame #7: 0x00000001000022a1 brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::__vector_base<Key, std::__1::allocator<Key> >::~__vector_base() + 21 at vector:452 [opt] 
    frame #8: 0x000000010000228c brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::vector<Key, std::__1::allocator<Key> >::~vector() at vector:457 [opt] 
    frame #9: 0x000000010000228c brute`Brute::subset_sum(std::__1::vector<Key, std::__1::allocator<Key> > const&, Key&, std::__1::vector<Key, std::__1::allocator<Key> > const&) [inlined] std::__1::vector<Key, std::__1::allocator<Key> >::~vector() at vector:457 [opt] 
    frame #10: 0x000000010000228c brute`Brute::subset_sum(this=0x0000000100400000, Table=<unavailable>, target=<unavailable>, solution=size=1) + 796 at brute.cpp:56 [opt] 
    frame #11: 0x0000000100002269 brute`Brute::subset_sum(this=0x0000000100400000, Table=<unavailable>, target=<unavailable>, solution=size=0) + 761 at brute.cpp:56 [opt] 
    frame #12: 0x0000000100002884 brute`main [inlined] Brute::decrypt(this=0x0000000100400000) + 56 at brute.cpp:27 [opt] 
    frame #13: 0x000000010000284c brute`main(argc=<unavailable>, argv=<unavailable>) + 44 at brute.cpp:97 [opt] 
    frame #14: 0x00007fffa51d9235 libdyld.dylib`start + 1 
    frame #15: 0x00007fffa51d9235 libdyld.dylib`start + 1 

这已经有一段时间,因为我在C++编码,所以我会很感激,如果熟悉该语言的人可以在我的方法中指出错误。

+2

你的代码的第二部分不返回任何东西 – user31264

回答

0

您的函数最后没有return声明。如果执行到达函数的末尾,程序将受到未定义的行为。

因为我没有清楚地理解函数中的逻辑,所以我无法提出解决方案。

相关问题