基于链表数组实现稀疏矩阵的C代码出现内存分配问题与段错误求助
稀疏矩阵链表实现段错误排查与修复
原始问题
- 实现逻辑:构建链表数组,数组下标对应矩阵行索引,单链表内存储对应行非零元素的列值与数据值
- 运行异常:触发内存分配异常、段错误,无法定位出错原因
- 原始问题代码:
#include <stdio.h> #include <stdlib.h> struct Node { int col; int data; struct Node *next; }*first; void Create(struct Node **A, int m, int n) { struct Node *t, *last; printf("Enter the elements of sparse matrix:\n"); for(int i=0; i<m; i++){ first = NULL; last = NULL; for(int j=0; j<n; j++){ int val; scanf("%d", &val); if(val != 0){ t= (struct Node *)malloc(sizeof(struct Node)); t->col = j; t->data = val; t->next = NULL; if(first) first = t; if(last) last->next = t; last = t; } } A[i] = first; } return; } void Display(struct Node **A, int m, int n){ printf("\nSparse matrix is:\n"); for(int i=0; i<m; i++){ struct Node *p = A[i]; for(int j=0; j<n; j++){ if(p->col == j){ printf(" %d ", p->data); if(p) p = p->next; } else printf(" 0 "); } printf("\n"); } } int main() { int m=5, n=6; struct Node **A = (struct Node **)malloc(m * sizeof(struct Node)); Create(A, m, n); Display(A, m, n); return 0; }
错误点排查
- 链表插入逻辑颠倒(核心段错误原因):
Create函数中first、last的判断逻辑完全写反。
原逻辑仅在first已存在时才将first指向新节点,导致每行第一个非零节点无法被first捕获,最终赋值给A[i]的first始终为NULL。后续Display函数直接对NULL指针执行p->col访问,触发空指针解引用段错误。
正确逻辑应为:仅当first为NULL(当前行无节点)时,才将first指向新节点;已有节点时直接追加到链表尾部。 - 显示函数空指针访问隐患:
Display函数中未判断p是否为NULL就直接访问p->col。即使修复了Create的逻辑,当某行节点全部遍历完成后p会变为NULL,后续循环仍会触发段错误。 - 内存分配逻辑不规范:
main函数中为二级指针A分配内存时,错误使用sizeof(struct Node)(节点结构体大小),实际需要分配m个struct Node*(指针)的空间。该问题虽不会直接触发段错误,但会造成内存浪费,属于不规范实现。 - 全局变量滥用:
first被声明为全局变量,虽每行循环时会重置,但不符合局部作用域设计规范,易被其他函数意外修改引发隐蔽bug。 - 无malloc返回值校验:所有
malloc调用均未检查返回值,若内存分配失败会返回NULL,后续对该指针的操作会触发不可预期的错误。 - 无内存释放逻辑:代码中未释放申请的堆内存,存在内存泄漏问题。
修复后代码
#include <stdio.h> #include <stdlib.h> struct Node { int col; int data; struct Node *next; }; void Create(struct Node **A, int m, int n) { struct Node *t, *last, *first; // 改用局部变量,避免全局变量污染 printf("Enter the elements of sparse matrix:\n"); for(int i = 0; i < m; i++) { first = NULL; last = NULL; for(int j = 0; j < n; j++) { int val; scanf("%d", &val); if(val != 0) { t = (struct Node *)malloc(sizeof(struct Node)); if (!t) { // 校验malloc返回值 perror("malloc failed"); exit(EXIT_FAILURE); } t->col = j; t->data = val; t->next = NULL; // 修正链表插入逻辑 if(!first) { // 当前行首个节点 first = t; last = t; } else { // 追加到链表尾部 last->next = t; last = t; } } } A[i] = first; } } void Display(struct Node **A, int m, int n) { printf("\nSparse matrix is:\n"); for(int i = 0; i < m; i++) { struct Node *p = A[i]; for(int j = 0; j < n; j++) { // 增加空指针判断,避免非法内存访问 if(p != NULL && p->col == j) { printf(" %d ", p->data); p = p->next; // 匹配到节点后直接移动指针,无需冗余判断 } else { printf(" 0 "); } } printf("\n"); } // 释放所有申请的堆内存,避免泄漏 for(int i = 0; i < m; i++) { struct Node *p = A[i]; while(p) { struct Node *temp = p; p = p->next; free(temp); } } free(A); } int main() { int m = 5, n = 6; // 修正二级指针的内存分配大小,按指针类型计算空间 struct Node **A = (struct Node **)malloc(m * sizeof(struct Node *)); if (!A) { perror("malloc failed"); exit(EXIT_FAILURE); } Create(A, m, n); Display(A, m, n); return 0; }
内容的提问来源于stack exchange,提问作者Manik Badshah
相关产品推荐
相关产品推荐

