我有一个结构的多维数组,其内容最初初始化为“\0”,后来偶尔初始化为“\0”。插入一些值后,按 fing_print
排序按升序排列。然后将排序后的数组的前 n 个条目复制到另一个数组中。在我的实现中,某些数组元素可能未分配值,因此它们包含之前分配给它们的“\0”值,并且在复制到另一个数组时需要忽略它们。
我的问题是,代码在“__DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1( errno_t, strcpy_s, _Post_z_ char, _Destination, _In_z_ char const*, _Source )处中断。
在调试时我注意到它在
<强> strcpy_s(selected[j][r].fing_print, hash_table[j][i].fing_print);
线
当j(桶)中只有一个包含数据时。
for (int j = 0; j < BUCKETS; ++j) {
// select rep fps here according to select size
for (int i = 0,r=0; i<B_ENTRIES, r < SELECT_SIZE; ++i) {
if (!strcmp(hash_table[j][i].fing_print, garb)) {
// garb contains '\0'
continue;
}
prev = i-1;
if (!strcmp(hash_table[j]i].fing_print,
hash_table[j][prev].fing_print)) {
// make sure the same fingerprint is not selected twice
continue;
}
strcpy_s(selected[j][r].fing_print,
hash_table[j][i].fing_print);
++r;
}
}
最佳答案
正如评论部分所述,我的错误是在循环退出条件 for (int i = 0,r=0; i<B_ENTRIES, r < SELECT_SIZE; ++i)
中使用逗号。 .将其更正为以下效果很好。
for (int j = 0; j < BUCKETS; ++j) {
// select rep fps here according to select size
for (int i = 0,r=0; i<B_ENTRIES && r < SELECT_SIZE; ++i) {
if (!strcmp(hash_table[j][i].fing_print, garb)) {
// garb contains '\0'
continue;
}
prev = i-1;
if (!strcmp(hash_table[j]i].fing_print,
hash_table[j][prev].fing_print)) {
// make sure the same fingerprint is not selected twice
continue;
}
strcpy_s(selected[j][r].fing_print,
hash_table[j][i].fing_print);
++r;
}
}
strcpy_s()
函数按原样工作正常,但正如上面的注释中指出的那样,它缺少一个参数,即目标数组的大小,因此应更正为
strcpy_s(selected[j][r].fing_print,sizeof(selected[j][r].fing_print),
hash_table[j][i].fing_print);
关于c - 以下 strcmp() 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46501057/