C++ 在 .size() 之后丢失向量元素
Posted
技术标签:
【中文标题】C++ 在 .size() 之后丢失向量元素【英文标题】:C++ losing vector elements after .size() 【发布时间】:2014-04-11 20:44:16 【问题描述】:我是 C++ 新手,我的问题是我以某种方式(似乎)仅通过调用 .size() 就丢失了向量元素。对于以下代码段(下面附有完整代码),我得到以下输出:
A: number of elements: 560
B: number of elements: 560
B: number of elements: 0
B: number of elements: 0
B: number of elements: 0
C: number of elements: 0
//有问题的部分
cout << "A: number of elements: " << combinations.size() << endl;
float sum_vect[120][2];
for (int i = 0; i < combinations.size(); ++i)
for (int j = 0; j < 2; ++j)
sum_vect[i][j] = 0;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
for (int i = 0; i < combinations.size(); ++i)
combination = combinations.at(i);
for (int j = 0; j < order; ++j)
sum_vect[i][0] += (float)virtual_pos[combination[j]][0];
sum_vect[i][1] += (float)virtual_pos[combination[j]][1];
vector<int> optimal_ind;
cout << "C: number of elements: " << combinations.size() << endl;
//辅助功能
void nchoosek_helper(int offset, int n, int k, vector<vector<int>> &combinations, vector<int> combination)
if (k == 0)
combinations.push_back(combination);
return;
for (int i = offset; i <= n - k; ++i)
combination.push_back(i);
nchoosek_helper(i+1, n, k-1, combinations, combination);
combination.pop_back();
double euclidean_norm(double dist1, double dist2)
return sqrt(pow(dist1,2) + pow(dist2,2));
//问题的功能从这里开始
//weirdest: look at A B C
vector<vector<char> > step37::CUDADriver::get_access_pattern(int order)
vector<vector<char> > result;
vector<vector<int> > combinations;
vector<int> combination;
nchoosek_helper(0, 16 ,order, combinations, combination);
cout << "number of combinations: " << combination.size() << endl;
// //mapping lexical index 1-16 to 2D array
int virtual_pos [16][2];
for (int i = 0; i < 16; ++i)
virtual_pos[i][0] = i%4 * order; //write x
virtual_pos[i][1] = (int)ceil(i/4) * order; //write y
cout << "mapping " << i << "to (" << i%4 * order << "'" << (int)ceil(i/4) * order<< ")" << endl;
cout << "A: number of elements: " << combinations.size() << endl;
float sum_vect[120][2];
for (int i = 0; i < combinations.size(); ++i)
for (int j = 0; j < 2; ++j)
sum_vect[i][j] = 0;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
cout << "B: number of elements: " << combinations.size() << endl;
for (int i = 0; i < combinations.size(); ++i)
combination = combinations.at(i);
for (int j = 0; j < order; ++j)
sum_vect[i][0] += (float)virtual_pos[combination[j]][0];
sum_vect[i][1] += (float)virtual_pos[combination[j]][1];
vector<int> optimal_ind;
cout << "C: number of elements: " << combinations.size() << endl;
cout << "main loop"<< endl;
for (int i = order; i < order*2; ++i)
for (int j = order; j < order*2; ++j)
int pos [2];
// pos[0] = i;
// pos[1] = j;
pos[0] = j;
pos[1] = i;
cout << "current position: (" << j << "," << i << ")" << endl;
float min_len = std::numeric_limits<float>::infinity(); //minimum length of combined vector
float min_sum_ind = std::numeric_limits<float>::infinity(); //minimum sum of individual vectors
int min_idx = -1;
for (int k = 0; k < combinations.size(); ++k)
int curr_vect [2];
curr_vect[0] = sum_vect[k][0] - pos[0] * order;
curr_vect[1] = sum_vect[k][1] - pos[1] * order;
float curr_len = euclidean_norm(curr_vect[0], curr_vect[1]);
float min_sum_tmp = 0;
combination = combinations[k];
for (int l = 0; l < order; ++l)
min_sum_tmp += euclidean_norm(virtual_pos[combination.at(l)][0] - pos[0],
virtual_pos[combination.at(l)][1]- pos[1]);
if (i==4&&j==4)
cout << " ind sum: " << min_sum_tmp << " len: " << curr_len << " sv: (" << sum_vect[k][0] << "," << sum_vect[k][1] <<
") cv: (" << curr_vect[0] << "," << curr_vect[1] << ")" <<endl;
if (min_len > curr_len ||
min_len == curr_len && min_sum_tmp < min_sum_ind)
min_len = curr_len;
min_idx = k;
min_sum_ind = min_sum_tmp;
// cout <<
cout << "pushing minimal idx " << min_idx << endl;
optimal_ind.push_back(min_idx);
cout << "main loop done"<< endl;
//unpack optimal combinations into relative movements
vector<char> optimal_x((int)pow(order,3));
vector<char> optimal_y((int)pow(order,3));
cout << "number of elements: " << combinations.size() << endl;
for (int i = 0; i <(int)pow(order,2); ++i)
cout << "optimal idx: " << optimal_ind.at(i) << endl;
combination = combinations.at(optimal_ind.at(i));
for (int j = 0; j < order; ++j)
int lex_idx = combination.at(j); //some index between 0 and 15 from 4x4 grid
//mvt range in grid relative to thread position: -1 to +
int relative_x = -1 + lex_idx % 4;
int relative_y = -1 + (int) floor(lex_idx / 4);
optimal_x[i * order + j] = relative_x;
optimal_y[i * order + j] = relative_y;
//DEBUG print
for (int i = 0; i < (int)pow(order,2); ++i)
combination = combinations.at(optimal_ind.at(i));
cout << "combination: " << i << " ";
for (int j = 0; j < order; ++j)
cout << combination.at(j) << " ";
cout << endl;
result.push_back(optimal_x);
result.push_back(optimal_y);
for (int i = 0; i < optimal_x.size(); ++i)
cout << (int)optimal_x.at(i) << " " << endl;
cout << "optimal sizes: " << optimal_x.size() << endl;
cout << "optimal sizes: " << optimal_y.size() << endl;
cout << "result size: " << result.size() << endl;
return result;
我真的不明白像 .size() 这样的函数怎么可能改变矢量对象(或者它可能只是同时发生)。该应用程序在单线程中运行,但我想无论如何都无关紧要,因为所有相关的内容都应该包含在函数的范围内。显然,当我实际尝试访问组合的某些元素(std::out_of_range)时,代码会在稍后停止工作。鉴于错误的严重性,我想我一定错过了一些非常基本的东西。令人不安的是,如果我使用 2 作为 get_access_pattern() 的参数,一切都会正常工作。以上所有内容(使用 3 和 4 测试)都会导致此错误。
【问题讨论】:
【参考方案1】:你有一个缓冲区溢出。你的输出:
A: number of elements: 560
您的代码:
cout << "A: number of elements: " << combinations.size() << endl;
float sum_vect[120][2];
for (int i = 0; i < combinations.size(); ++i)
for (int j = 0; j < 2; ++j)
sum_vect[i][j] = 0;
查看“i”变量在该循环中达到 120 时的值。您正在访问 sum_vect[120][j],这是超出范围的。
当发生缓冲区溢出时,您的程序将表现出未定义的行为。
【讨论】:
谢谢,我完全忘记了我在处理另一个错误时放了一个常量。我想这就是人们告诉我尽可能使用向量的原因之一。非常感谢!未定义的行为...实际上像时尚一样以定时炸弹的形式出现错误真的很可怕。
std::vector<std::vector<float>> sum_vect(combinations.size(), std::vector<float>(2,0));
应该这样做,并删除循环以将数据初始化为0。以上是关于C++ 在 .size() 之后丢失向量元素的主要内容,如果未能解决你的问题,请参考以下文章