我正在创建一个整数向量来存储一些值。但是,下面的代码似乎根本没有更新向量。错误信息:
malloc: *** error for object 0x200000001: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
代码:
#include <vector>
#include <iostream>
#include "Q122.h"
void Q122::solve() {
std::vector<int> count(200, 0);
std::vector<std::vector<std::vector<int> > > wholelist(200);
count[0] = 0;
std::vector<int> one;
one.push_back(1);
wholelist[0].push_back(one);
int totalsum = 0;
int index = 0;
while(index != 199) {
int currentstep = count[index];
int currentpower = index+1;
std::vector<std::vector<int> > list = wholelist[index];
for(std::vector<int> each : list) {
for(int number : each) {
std::vector<int> copied(each);
int nextindex = number + currentpower - 1;
copied.push_back(nextindex + 1);
if(count[nextindex] == 0) {
count[nextindex] = currentstep + 1;
wholelist[nextindex].push_back(copied);
continue;
}
if(count[nextindex] != 0 && count[nextindex] > currentstep + 1) {
count[nextindex] = currentstep + 1;
wholelist[nextindex].clear();
wholelist[nextindex].push_back(copied);
continue;
}
if(count[nextindex] == currentstep + 1) {
wholelist[nextindex].push_back(copied);
continue;
}
}
}
std::cout << count[index] << std::endl;
index++;
}
int sum = 0;
for(int each : count)
sum += each;
std::cout << sum << std::endl;
}
这个错误信息是什么意思?顺便说一句,如何检查向量的内容?我在 netbeans Mac OS 上使用 clang。
解决了。
问题已解决:我忘了检查 nextindex 的边界。