循环 C++ 中丢失的第一个数组元素
First array elements lost in loop C++
我遇到了一个奇怪的行为。我在循环中处理一些数据,然后如果它满足某些条件,我将值放入一个数组,但由于我不知道满足条件的数据的大小,我的声明是 double arr[][4] = {0}
(如果不已初始化,我收到错误消息)。
通过测试数据,我知道大小必须是 1852 x 4。问题是,如果我在函数内定义数组,前 52 个元素在一些循环后变为 0,但总是前 52 个。
使用 printf
,我发现第一个元素在 y
循环更改后丢失,以及在该“循环周期”中处理的另一个元素,但我没有触及数组变量在 IF 语句之外,所以,我不明白为什么只有第一个数据丢失,那些循环没有什么特别的。
此外,如果我在函数外声明数组,不会丢失任何数据,但在收集到 83 个元素后,我收到错误“分段错误(核心已转储)”。
总代码量太大,但涉及到如下:
double arr[][4] = {0}; // if declared here Segmentation Fault
float process(data){
double arr[][4] = {0}; // if declared here first elements losing
for(int y = 0; y < 800; y++){
for(int x = 0; x < 500; x++){
/* do some stuff */
if (prop>=criteria and prop<=criteria2){
arr[cont][0] = prop;
arr[cont][1] = y;
arr[cont][2] = xx;
arr[cont][3] = pixp;
cont++;
printf("%lf %lf %lf %lf \n", arr[0][0], arr[0][1], arr[0][2], arr[0][2]);
//looking for the losing moment of first row
}
}
}
}
此外,如果我声明一个数组“足够大”,例如 arr[20000][4]
,我仍然会丢失数据或出现分段错误,但会出现更奇怪的行为,因为分段错误出现在收集到的 2105 个元素(更多比实际符合标准的)。
C++ 数组不会动态增长。您超出了数组的范围,这会导致 未定义的行为。
如果您事先不知道要处理多少个元素,请改用 std::vector
。在这种情况下,我会说改用 struct
元素的向量,因为每个循环迭代要存储 4 double
个值,例如:
#include <vector>
struct myValues {
double prop;
double y;
double xx;
double pixp;
};
float process(data){
std::vector<myValues> arr;
for(int y = 0; y < 800; y++){
for(int x = 0; x < 500; x++){
/* do some stuff */
if (prop >= criteria && prop <= criteria2){
myValues val;
val.prop = prop;
val.y = y;
val.xx = xx;
val.pixp = pixp;
arr.push_back(val);
printf("%lf %lf %lf %lf \n", val.prop, val.y, val.xx, val.pixp);
}
}
}
}
我遇到了一个奇怪的行为。我在循环中处理一些数据,然后如果它满足某些条件,我将值放入一个数组,但由于我不知道满足条件的数据的大小,我的声明是 double arr[][4] = {0}
(如果不已初始化,我收到错误消息)。
通过测试数据,我知道大小必须是 1852 x 4。问题是,如果我在函数内定义数组,前 52 个元素在一些循环后变为 0,但总是前 52 个。
使用 printf
,我发现第一个元素在 y
循环更改后丢失,以及在该“循环周期”中处理的另一个元素,但我没有触及数组变量在 IF 语句之外,所以,我不明白为什么只有第一个数据丢失,那些循环没有什么特别的。
此外,如果我在函数外声明数组,不会丢失任何数据,但在收集到 83 个元素后,我收到错误“分段错误(核心已转储)”。
总代码量太大,但涉及到如下:
double arr[][4] = {0}; // if declared here Segmentation Fault
float process(data){
double arr[][4] = {0}; // if declared here first elements losing
for(int y = 0; y < 800; y++){
for(int x = 0; x < 500; x++){
/* do some stuff */
if (prop>=criteria and prop<=criteria2){
arr[cont][0] = prop;
arr[cont][1] = y;
arr[cont][2] = xx;
arr[cont][3] = pixp;
cont++;
printf("%lf %lf %lf %lf \n", arr[0][0], arr[0][1], arr[0][2], arr[0][2]);
//looking for the losing moment of first row
}
}
}
}
此外,如果我声明一个数组“足够大”,例如 arr[20000][4]
,我仍然会丢失数据或出现分段错误,但会出现更奇怪的行为,因为分段错误出现在收集到的 2105 个元素(更多比实际符合标准的)。
C++ 数组不会动态增长。您超出了数组的范围,这会导致 未定义的行为。
如果您事先不知道要处理多少个元素,请改用 std::vector
。在这种情况下,我会说改用 struct
元素的向量,因为每个循环迭代要存储 4 double
个值,例如:
#include <vector>
struct myValues {
double prop;
double y;
double xx;
double pixp;
};
float process(data){
std::vector<myValues> arr;
for(int y = 0; y < 800; y++){
for(int x = 0; x < 500; x++){
/* do some stuff */
if (prop >= criteria && prop <= criteria2){
myValues val;
val.prop = prop;
val.y = y;
val.xx = xx;
val.pixp = pixp;
arr.push_back(val);
printf("%lf %lf %lf %lf \n", val.prop, val.y, val.xx, val.pixp);
}
}
}
}