在VS2019中,调试时代码运行过几次之后总会提示:
引发了未经处理的异常:读取访问权限冲突。
相关代码片段如下
struct s_list {
int data_num;
float data_pow;
struct s_list* next;
};
s_list* head = NULL;
head = (struct s_list*)malloc(sizeof(struct s_list));
s_list* p1 = (struct s_list*)malloc(sizeof(struct s_list));
s_list* hq = NULL, * q = NULL;
if (head->data_num < 0) {
head->data_num = b1[n][m].Bsta[k].num;
head->data_pow = p[n][m].Bsta_pow[k];
}
else {
p1->data_num = b1[n][m].Bsta[k].num;
p1->data_pow = p[n][m].Bsta_pow[k];
}
//插入节点
if (p1 != NULL && p1->data_num > 0) {
if (head->data_pow > p1->data_pow) {
q = head;
while (q != NULL && q->next != NULL && q->data_pow > p1->data_pow) {
hq = q;
if (q->next == NULL)break;
q = q->next;
}
if (q->data_pow < p1->data_pow) {
if (hq != NULL)
hq->next = p1;
p1->next = q;
}
if ((q->data_pow > p1->data_pow) && (q->next == NULL)) {
q->next = p1;
}
}
if (head->data_pow < p1->data_pow) {
p1->next = head;
head = p1;
}
}
}
指针的有效性验证不完全,while循环退出的一种情况是q=NULL,而while之后的if中取q->data_pow,这就发生错误了
不知道你这个问题是否已经解决, 如果还没有解决的话: