You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言程序无编译错误但触发Thread 1: signal SIGABRT问题求助

图书信息管理程序SIGABRT错误排查与解决

问题情况

  • 运行环境:M2 MacBook Air + 最新版Xcode
  • 触发场景:选择模式1添加图书信息(所有项输入aaa),选择模式3结束程序时,main函数的return 0行触发Thread 1: signal SIGABRT错误,无编译错误
  • 程序代码:
#include <stdio.h>
// base
int str_length(char *pstr);
int str_copy(char *pstr_c, char *pstr_v);
int str_compare(char *pstr_a, char *pstr_b);
int str_add(char *pstr, char *pstr_add);

// program
int book_add(char (*pbook)[4][100], int row);
int book_search(char (*pbook)[4][100], int row);

int main() {
    char book[100][4][100];
    int ans, row = 0, end = 0;
    
    while (1) {
        printf("Choose the mode (Add 1, search 2, end 3) : ");
        scanf("%d", &ans);
        switch (ans) {
            case 1 :
                book_add(book, 100);
                row++;
                break;
            case 2 :
                book_search(book, 100);
                break;
            case 3 :
                end = 1;
            default :
                break;
        }
        if (end) { break; }
    }
    return 0;
}

// base
int str_length(char *pstr) {
    int i = 0;
    while (pstr[i]) {
        i++;
    }
    return i;
}
int str_copy(char *pstr/*문자*/, char *pstr_c/*복사 문자*/) {
    while (*pstr_c) {
        *pstr = *pstr_c;
        pstr_c++;
        pstr++;
    }
    *pstr = '\0';
    return 1;
}
int str_compare(char *pstr_a, char *pstr_b) {
    while (*pstr_a) {
        if (*pstr_a != *pstr_b) { return 0; }
        pstr_a++;
        pstr_b++;
    }
    if (*pstr_b != 0) { return 0; }
    return 1;
}
int str_add(char *pstr /*문자*/, char *pstr_add /*더할 문자*/) {
    while (*pstr) {
        pstr++;
    }
    while (*pstr_add) {
        *pstr = *pstr_add;
        pstr++;
        pstr_add++;
    }
    *pstr = '\0';
    return 1;
}
int str_clear(char *pstr, int row) {
    while (row) {
        *pstr = '\0';
        pstr++;
        row--;
    }
    return 1;
}

// program
int book_add(char (*pbook)[4][100], int row) {
    printf("Title : ");
    scanf("%s", &pbook[row][0][0]);
    printf("Auther : ");
    scanf("%s", &pbook[row][1][0]);
    printf("Publisher : ");
    scanf("%s", &pbook[row][2][0]);
    str_copy(&pbook[row][3][0], "available");
    return 1;
}
int book_search(char (*pbook)[4][100], int row){
/*I didn't make it yet*/
    return 1;
}

错误原因

  1. 数组越界访问(核心原因):
    main函数调用book_add时传入固定值100,但book数组的合法索引范围是0~99。book_add用100作为下标访问pbook[100],属于越界写入,破坏了栈内存中的其他数据(比如main的局部变量、函数返回地址),最终导致程序退出时触发SIGABRT信号。
  2. 冗余地址操作:
    scanf("%s", &pbook[row][0][0])中的&是多余的,pbook[row][0]本身就是指向字符串首元素的指针,额外取地址会导致类型不匹配,虽未直接报错但不符合规范。

解决方法

  1. 修正函数调用参数:
    在main的switch分支中,调用book_add和book_search时传入当前记录已添加图书数量的row变量,而非固定值100:
    case 1 :
        book_add(book, row);
        row++;
        break;
    case 2 :
        book_search(book, row);
        break;
    
  2. 优化地址传参:
    修改book_add中的scanf和str_copy调用,去掉多余的&:
    int book_add(char (*pbook)[4][100], int row) {
        printf("Title : ");
        scanf("%s", pbook[row][0]);
        printf("Auther : ");
        scanf("%s", pbook[row][1]);
        printf("Publisher : ");
        scanf("%s", pbook[row][2]);
        str_copy(pbook[row][3], "available");
        return 1;
    }
    
  3. 后续优化提示:
    实现book_search功能时,用传入的row值作为遍历上限,只处理已添加的图书数据,避免遍历未初始化的数组元素。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 23:05:01