`/tmp/Main' 中的错误:free():无效的下一个大小(快速):没有动态分配
Error in `/tmp/Main': free(): invalid next size (fast): No dynamic allocation
所以,我在 lintcode 中做一个名为 "Unique Paths II" 的问题。但是,我在测试用例中遇到了运行时错误:
// in the context, map is the 2d array that saves the obstacle path as described in the question, which fits common code practices
for (int i = 1; i < n; i++) {
if (map[0][i] != 1)
map[0][i] = 1;
else{
for(;i<m;i++){
map[0][i] = 0;
}
break;
}
}
Lintcode 给我这个错误:
*** Error in `/tmp/Main': free(): invalid next size (fast): 0x0000000001bb5130 *** ======= Backtrace: ========= [0x625861] [0x62dda6] [0x631dd7] [0x40f00a] [0x40c78e] [0x409cda] [0x407823] [0x4062a7] [0x410cd9] [0x40e361] [0x40bbb0] [0x40912d] [0x406ca1] [0x405b96] [0x4031ce] [0x6038c6] [0x603aba] [0x400d69] ======= Memory map: ======== 00400000-007d9000 r-xp 00000000 ca:01 890688 /tmp/Main 009d8000-009e4000 rw-p 003d8000 ca:01 890688 /tmp/Main 009e4000-009e9000 rw-p 00000000 00:00 0 01b92000-01bd6000 rw-p 00000000 00:00 0 [heap] 7f5660000000-7f5660047000 rw-p 00000000 00:00 0 7f5660047000-7f5664000000 ---p 00000000 00:00 0 7f5666690000-7f5666691000 rw-p 00000000 00:00 0 7ffea1f83000-7ffea1fa4000 rw-p 00000000 00:00 0 [stack] 7ffea1ff0000-7ffea1ff3000 r--p 00000000 00:00 0 [vvar] 7ffea1ff3000-7ffea1ff5000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped)
这里有什么问题?
无论错误如何,您都应该阅读 Why is “using namespace std;” considered bad practice? 并启用编译器警告。在你的代码中有很多
warning: implicit conversion changes signedness: 'int' to 'size_type' (aka 'unsigned long') [-Wsign-conversion]
和
warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
您可以使用
轻松修复它
auto m = map.size(), n = map[0].size();
和
for (decltype(n) i = 1; i < n; i++) {
您可以使用 lldb 调试用 clang 编译的代码。
实际错误在
部分
for (int i = 1; i < n; i++) {
if (map[0][i] != 1)
map[0][i] = 1;
else{
for(;i<m;i++){
map[0][i] = 0;
}
break;
}
}
在外循环中 i
从 0
迭代到 n
但在内循环中 i
从 0
迭代到 m
和
map[0][i]
出界。
所以,我在 lintcode 中做一个名为 "Unique Paths II" 的问题。但是,我在测试用例中遇到了运行时错误:
// in the context, map is the 2d array that saves the obstacle path as described in the question, which fits common code practices
for (int i = 1; i < n; i++) {
if (map[0][i] != 1)
map[0][i] = 1;
else{
for(;i<m;i++){
map[0][i] = 0;
}
break;
}
}
Lintcode 给我这个错误:
*** Error in `/tmp/Main': free(): invalid next size (fast): 0x0000000001bb5130 *** ======= Backtrace: ========= [0x625861] [0x62dda6] [0x631dd7] [0x40f00a] [0x40c78e] [0x409cda] [0x407823] [0x4062a7] [0x410cd9] [0x40e361] [0x40bbb0] [0x40912d] [0x406ca1] [0x405b96] [0x4031ce] [0x6038c6] [0x603aba] [0x400d69] ======= Memory map: ======== 00400000-007d9000 r-xp 00000000 ca:01 890688 /tmp/Main 009d8000-009e4000 rw-p 003d8000 ca:01 890688 /tmp/Main 009e4000-009e9000 rw-p 00000000 00:00 0 01b92000-01bd6000 rw-p 00000000 00:00 0 [heap] 7f5660000000-7f5660047000 rw-p 00000000 00:00 0 7f5660047000-7f5664000000 ---p 00000000 00:00 0 7f5666690000-7f5666691000 rw-p 00000000 00:00 0 7ffea1f83000-7ffea1fa4000 rw-p 00000000 00:00 0 [stack] 7ffea1ff0000-7ffea1ff3000 r--p 00000000 00:00 0 [vvar] 7ffea1ff3000-7ffea1ff5000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted (core dumped)
这里有什么问题?
无论错误如何,您都应该阅读 Why is “using namespace std;” considered bad practice? 并启用编译器警告。在你的代码中有很多
warning: implicit conversion changes signedness: 'int' to 'size_type' (aka 'unsigned long') [-Wsign-conversion]
和
warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
您可以使用
轻松修复它auto m = map.size(), n = map[0].size();
和
for (decltype(n) i = 1; i < n; i++) {
您可以使用 lldb 调试用 clang 编译的代码。
实际错误在
部分for (int i = 1; i < n; i++) {
if (map[0][i] != 1)
map[0][i] = 1;
else{
for(;i<m;i++){
map[0][i] = 0;
}
break;
}
}
在外循环中 i
从 0
迭代到 n
但在内循环中 i
从 0
迭代到 m
和
map[0][i]
出界。