将元素添加到现有结构会使函数在分段错误时崩溃
adding element to an existing struct makes function crash on segmentation error
我有以下结构:
struct FeatureMatch {
int id1, id2;
double score;
};
当被此函数调用时运行良好:
void ssdMatchFeatures(const FeatureSet &f1, const FeatureSet &f2, vector<FeatureMatch> &matches, double &totalScore) {
int m = f1.size();
int n = f2.size();
matches.resize(15000);
totalScore = 0;
double d;
double dBest;
int idBest;
printf("2");
for (int i=0; i<m; i++) {
dBest = 1e100;
idBest = 0;
for (int j=0; j<n; j++) {
d = distanceSSD(f1[i].data, f2[j].data);
if (d < dBest) {
dBest = d;
idBest = f2[j].id;
}
}
printf("1n");
matches[i].id1 = f1[i].id;
matches[i].id2 = idBest;
matches[i].score = -dBest;
// matches[i].second=1;
totalScore += matches[i].score;
}
printf("3");
}
但是,一旦我通过添加新元素来修改结构:
struct FeatureMatch {
int id1, id2;
double score, second;
};
称为 second 的额外双精度值会使上述函数在分段错误时崩溃。1,2,3 的输出如下所示:
211111...1
但在崩溃之前永远不会达到 3。
这是怎么回事?即使我从不修改匹配[i].second,也会发生这种情况。
如果更改结构定义,则应重新生成所有受影响的源文件(如果生成工具未自动执行此操作)。否则,您可能会将使用不同结构定义的模块链接在一起,这将导致令人困惑的错误(如您所见)。
相关文章:
- 尝试使用集合函数时出现分段错误
- 在函数 strcpy() 中访问字符数组时出现分段错误
- 分段 排序函数实现中的错误
- 在 C 函数中返回结构会导致分段错误
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 分段故障 11,从类函数显示动态 C 字符串
- 默认构造函数上的分段错误
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 输入处理程序函数中的分段错误
- 在类和构造函数中使用向量时出现分段错误
- 分段错误分配函数结果
- 使用getline和字符串函数C++程序中的分段错误
- 析构函数分段错误链表
- GDB 仅显示发生错误的函数(分段错误)
- 构造函数分段错误
- C++析构函数分段错误
- 节点和二叉树构造函数和析构函数分段
- 调用虚函数-分段故障
- 访问派生类中受保护的成员函数--分段错误