C++ 为什么这个递归子集和算法会导致指针分配错误?

C++ 为什么这个递归子集和算法会导致指针分配错误?,c++,algorithm,pointers,lldb,subset-sum,C++,Algorithm,Pointers,Lldb,Subset Sum,好的,下面是我的子集和算法实现: 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.begi

好的,下面是我的子集和算法实现:

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);
    }
}
std::vector Brute::subset_sum(const std::vector&Table、Key&target、const std::vector&solution){
键和=键();
对于(std::vector::const_迭代器it=solution.begin();it!=solution.end();it++){
总和+=*它;
}
如果(总和==目标){
回流液;
}
如果(目标<总和){
返回std::vector();
}
钥匙;
对于(std::vector::const_迭代器it=Table.begin();it!=Table.end();it++){
key=*it;
std::向量剩余;
for(std::vector::const_迭代器jt=it;jt!=Table.end();jt++){
如果(jt==it){
继续;
}
剩余。将_向后推(*it);
}
std::矢量溶胶=溶液;
溶胶。推回(键);
子集和(剩余、目标、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)
brute(1465,0x7fffae14d3c0)malloc:**对象0x7fff5fbff6b0的错误:未分配要释放的指针
***在malloc\u error\u break中设置断点以进行调试
进程1465已停止
*线程#1:tid=0x9ee7,0x00007fffa5307d42 libsystem_kernel.dylib`u pthread_kill+10,队列='com.apple.main thread',停止原因=信号SIGABRT
帧#0:0x00007fffa5307d42 libsystem_kernel.dylib`\uu pthread_kill+10
libsystem\u kernel.dylib`\u pthread\u kill:
->0x7fffa5307d42:jae 0x7fffa5307d4c;
0x7fffa5307d44:movq%rax,%rdi
0x7fffa5307d47:jmp 0x7fffa5300caf;塞罗鲁诺坎塞尔
0x7fffa5307d4c: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
(lldb)bt
*线程#1:tid=0x9ee7,0x00007fffa5307d42 libsystem_kernel.dylib`u pthread_kill+10,队列='com.apple.main thread',停止原因=信号SIGABRT
*帧#0:0x00007fffa5307d42 libsystem_kernel.dylib`\uu pthread_kill+10
帧#1:0x00007fffa53f55bf libsystem_pthread.dylib`pthread_kill+90
帧#2:0x00007fffa526d420 libsystem_c.dylib`abort+129
帧#3:0x00007fffa535cfe7 libsystem_malloc.dylib`free+530
帧#4:0x00000001000022a6 brute`brute::subset(std::uu 1::vector const&,Key&,std:u 1::vector const&)[内联]std:u 1::u解除分配(u ptr=)+822新:177[opt]
帧#5:0x00000001000022a1 brute`brute::subset_sum(std::uu 1::vector const&,Key&,std:u 1::vector const&)std::u 1::allocator::deallocate(u p=)内存:1731[opt]
帧#6:0x00000001000022a1 brute`brute::subset_sum(std::uu 1::vector const&,Key&,std:u 1::vector const&)std:u 1::allocator_traits::deallocate(u p=)内存:1496[opt]
帧#7:0x00000001000022a1 brute`brute::subset_sum(std::uu 1::vector const&,Key&,std:u 1::vector const&)std:u 1::u vector_base::~u vector_base()+21 at vector:452[opt]
帧#8:0x000000010000228c brute`brute::subset_sum(std::uu 1::vector const&,Key&,std:u 1::vector const&)[内联]std::u 1::vector::~vector()在向量:457[opt]
帧#9:0x000000010000228c brute`brute::subset_sum(std::uu 1::vector const&,Key&,std:u 1::vector const&)[内联]std::u 1::vector::~vector()在向量:457[opt]
帧#10:0x000000010000228c brute`brute::subset_sum(这=0x0000000100400000,Table=,target=,solution=size=1)+796在brute.cpp:56[opt]
帧#11:0x0000000100002269 brute`brute::subset_sum(这=0x0000000100400000,Table=,target=,solution=size=0)+761在brute.cpp:56[opt]
帧#12:0x0000000100002884 brute`main[inlined]brute::decrypt(this=0x0000000100400000)+56 at brute.cpp:27[opt]
帧#13:0x000000010000284c brute`main(argc=,argv=)+44在brute.cpp:97[opt]
帧#14:0x00007fffa51d9235 libdyld.dylib`start+1
帧#15:0x00007fffa51d9235 libdyld.dylib`start+1

自从我用C++编写代码已经有一段时间了,所以如果我熟悉的语言能指出我的方法中的错误,我会很感激。

< P>你的函数在结尾没有一个<代码>返回< /C> >语句。如果执行到函数末尾,程序将受到未定义行为的约束


我无法提出解决方案,因为我不清楚函数中的逻辑。

代码的第二部分没有返回任何内容