【发布时间】:2018-04-03 18:11:25
【问题描述】:
我正在编写一个程序,将两个矩阵相乘,第三个矩阵打印结果。除此之外,我想计算我在下面的代码中指定的算术运算时间,但是我不断遇到分段错误,我相信我正确使用了 time.h/clock() 库函数。我提供了给我问题的部分的 sn-p。
/** Third Matrix **/
for (i = 0; i < N; i++) {
for (x = 0; x < N; x++) {
arr3[i][x] = 0;
for (y = 0; y < N; y++)
arr3[i][x] = arr3[i][x] + arr1[i][y] * arr2[y][x];
}
}
arithmeticBegin = clock(); //begins the clock for arithmetic time
//the following line is what was causing the seg fault
arr3[i][x] = arr3[i][x] + arr1[i][y] * arr2[y][x];
arithmetic_endTime = clock(); //stops the clock for the end of arithmetic time
/** The following computers total arithmetic Time **/
arithmeticTime += (double)(arithmetic_endTime - arithmeticBegin) / CLOCKS_PER_SEC;
【问题讨论】:
-
没有minimal reproducible example 就无法回答。
-
@FredLarson 我已经添加了完整的代码。
-
程序中的什么地方出现了分段错误?使用 gdb 查找。
-
@ArndtJonasson 我已经评论了导致分段错误的行。
标签: c segmentation-fault malloc clock time.h