为什么这种递归子集和算法会导致指针分配错误
Why is this recursive subset sum algorithm causing a pointer allocation error?
好的,这是我的子集和算法实现:
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++编码以来已经有一段时间了,所以如果熟悉该语言的人可以指出我的方法中的错误,我将不胜感激。
你的函数末尾没有 return
语句。如果执行到达函数的末尾,您的程序将受到未定义行为的影响。
我无法提出解决方案,因为我不清楚您的函数中的逻辑。
相关文章:
- 将指针分配给另一个指针时会发生什么情况?
- C++将一个指针分配给另一个指针时执行的类型检查
- 我似乎无法为指针分配一个数组,然后更改数组的内容
- C++为 void 指针分配一个字符串
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- MPI 集合通信中的指针分配
- 函数指针分配在创建 DLL 后导致错误
- 通过双指针分配指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- 我可以直接为指针分配地址吗?如果是,如何做到这一点
- 为什么为char指针分配内存不会截断char序列
- C++指针分配字符数组的地址
- 将 void 指针分配给另一个 void 指针
- 如何将不同的成员函数指针分配给注册类的不同实例?
- 如何为指针分配特定的内存地址?
- C++ - 无法将顶部常量指针分配给另一个非常量指针
- 将指针分配给另一个指针时会发生什么情况?
- 为指向C++中的对象/对象的指针分配堆内存
- 将指针分配给字符串变量中包含的地址
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用