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
를 역 추적이다 : 나는 C++ 코딩 이후
(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
그것은 잠시 있었다, 그래서 나는 경우 감사하겠습니다 언어에 익숙한 사람이 여기 내 접근 방식에서 잘못을 지적 할 수 있습니다.
코드의 두 번째 부분은 아무 것도 반환하지 않습니다. – user31264