You need to enable JavaScript to run this app. C语言开发带Mutex并行的矩阵处理程序:编译通过执行遇Valgrind错误及段错误
优惠活动
大模型
产品
解决方案
定价
更多

C语言开发带Mutex并行的矩阵处理程序:编译通过执行遇Valgrind错误及段错误

C语言矩阵二进制处理程序开发记录

开发目标

开发一款C语言程序,功能为从二进制输入文件读取多组矩阵,将二进制格式的计算结果写入输出文件。程序需实现的核心逻辑:

  • 完成相邻两个矩阵的乘法运算
  • 乘法计算结束后对结果执行排序算法
  • 后续迭代计划加入Mutex实现并行性能优化

当前代码待修复的已知问题

  • mult_matrices() 函数未实现两个矩阵的正确运算逻辑
  • show_matrices() 函数未正确写入矩阵维度头信息,也未写入文件末尾的0-0结束标记
  • 程序的各阶段耗时、总耗时计算逻辑存在错误

编译与运行命令

# 编译命令
gcc -Wall -Werror -O2 matrix.c -o mm
# 带内存检测的运行命令
valgrind --leak-check=yes -q --vgdb=no ./mm input output

运行报错详情

程序可正常完成编译,但执行时输出如下结果:

Reading:         1902900ns
==54== Use of uninitialised value of size 8
==54==    at 0x10962C: mult_matrices (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==    by 0x10922A: main (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==
==54== Invalid read of size 4
==54==    at 0x10962C: mult_matrices (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==    by 0x10922A: main (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==54==
==54==
==54== Process terminating with default action of signal 11 (SIGSEGV)
==54==  Access not within mapped region at address 0x0
==54==    at 0x10962C: mult_matrices (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==    by 0x10922A: main (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==  If you believe this happened as a result of a stack
==54==  overflow in your program's main thread (unlikely but
==54==  possible), you can try to increase the size of
==54==  main thread stack using the --main-stacksize= flag.
==54==  The main thread stack size used in this run was 8388608.
==54== 800 bytes in 1 blocks are definitely lost in loss record 6 of 6
==54==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==54==    by 0x1095B9: mult_matrices (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==    by 0x10922A: main (in /mnt/c/Source/Operating-Systems/matrix_Manipulation/mm)
==54==
Segmentation fault (core dumped)

内容的提问来源于stack exchange,提问作者rebelhockey55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 06:24:03