这是我正在执行的代码(简化为最小的工作示例):
#include <cstdio>
static unsigned long long n = 4;
static unsigned long long k = 4;
int main() {
for (unsigned long long i = 2; i < k; ++i) {
for (unsigned long long j = 2; j < n; ++j) {
unsigned long long* v = new unsigned long long[j];
for (unsigned long long h = 0; h < j; ++i)
v[h] = h + 1;
delete[] v;
}
}
return 0;
}
当我运行此代码时,它不会终止。使用gdb
似乎卡在指令unsigned long long* v = new unsigned long long[j]
上。使用display
命令,j
的值为2
。
我的问题是它为什么会被困在那条线上。
gdb信息
Reading symbols from main...done.
(gdb) disp n
1: n = <optimized out>
(gdb) disp k
2: k = 4
(gdb) break main
Breakpoint 1 at 0x400a80: file main.cpp, line 6.
(gdb) run Starting program: /home/user/cpp/main
Breakpoint 1, main () at main.cpp:6
6 int main() {
2: k = 4
1: n = <optimized out>
(gdb) s
11 unsigned long long* v = new unsigned long long[j];
2: k = 4
1: n = <optimized out>
(gdb) disp j
3: j = 2
(gdb) s
被困在这里。
答案 0 :(得分:3)
你在内循环中增加i而不是h