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

C程序可运行但GDB在进入main前崩溃,求助调试方案(基于GSL)

基于GSL的C程序调试及运行异常问题

问题概述

我正在开发一个依赖GSL库的C程序,用于加载两个CSV文件作为矩阵并执行运算。程序编译后运行,会提示输入文件名,但输入后进入无限循环。尝试用GDB调试时,GDB在进入main函数前就触发SIGSEGV错误,无法进行回溯调试。

我在Mac系统上通过装有GDB和Valgrind的Linux容器调试,但GDB和Valgrind均在进入main函数前触发SIGSEGV,无法继续。仅使用-Wall和-g编译选项时,程序能运行到加载矩阵的阶段。我需要先解决调试入口的问题,进入main函数调试,进而排查load_data函数的问题。

相关代码

matrixoperations.c

#include "datafile.h"
#include <stdio.h>
#include <string.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_errno.h>

int main(){
    
    int M = 2;
    int N = 2;
    int O = 0;
    int P = 0;
    char file1[256];
    char file2[256];
    
    printf("Please specify the name of the csv for the first matrix. \n");
    scanf("%255s", file1);
    getchar();
    printf("Please specify the name the csv file for the second matrix. \n");
    scanf("%255s", file2);
    getchar();
    printf("%s\n", file1);

    gsl_matrix *A = gsl_matrix_alloc(M,N);
    gsl_matrix *B = gsl_matrix_alloc(O,P);

    // File loading of Matrix Values.
    count_rows_cols(file1, &M, &N);
    count_rows_cols(file2, &O, &P);

    if( M == N && N == O && N == P){
        load_data(file1, A);
        load_data(file2, B);
    } else {
        printf("You must provide 2 square matrixes for this program as of now.\n");
    }

    //gsl_matrix_add(A, B);
    gsl_matrix_mul_elements(A,B);
    double max = gsl_matrix_max(A);
    gsl_matrix_transpose(B);

 ......}

datafile.c

#include "datafile.h"
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_errno.h>


void count_rows_cols(const char *filename, int *rows, int *cols){
    FILE *file = fopen(filename, "r");
    if (!file){
        perror("Unable to open the file");
        log_err("Unable to open file: name = %s ", filename);
        exit(1);
    }

    //char buffer[1024];
    char* buffer= malloc(1024*sizeof(char));
    int row_count = 0;
    int col_count =0;

    while(fgets(buffer, 1024, file)){
        row_count++;
        if (row_count == 1){
            char *token = strtok(buffer, ",");
            while (token){
                col_count++;
                token = strtok(NULL, ",");
            }
        }
    }
    fclose(file);

    *rows = row_count;
    *cols = col_count;
    if(row_count != col_count){
        log_err("Not a square matrix: row:column =  %d:%d name = %s ", row_count, col_count, filename);
    }
    free(buffer);

}

void load_data(const char *filename, gsl_matrix *m){
    FILE *file = fopen(filename, "r");
    if(!file){
        perror("Unable to open the file");
        log_err("Unable to open file: name = %s ", filename);
        exit(1);
    }

    int row = 0; 
    //char buffer[1024];
    char* buffer= malloc(1024*sizeof(char));

    while (fgets(buffer, 1024, file)){
        int col = 0;
        char *value = strtok(buffer, ",");

        while (value){
            if (row >= 0 && row < (int)m->size1 && col >= 0 && col < (int)m->size2) {
                gsl_matrix_set(m, row, col, atof(value));
                value = strtok(NULL, ",");
                col++;
            } else {
            log_err("Attempt to access [%d, %d] out of matrix bounds.\n", row, col);
            }
        }
        row++;
    }
    fclose(file);
    free(buffer);
}

datafile.h

#ifndef __datafile_h__
#define __datafile_h__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsl/gsl_matrix.h>

void count_rows_cols(const char *filename, int *rows, int *cols);

void load_data(const char *filename, gsl_matrix *m);

#endif

Valgrind输出

make: Circular build/ScienceDog <- build/ScienceDog dependency dropped.
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./build/ScienceDog
==232== Memcheck, a memory error detector
==232== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==232== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==232== Command: ./build/ScienceDog
==232== 
==232== 
==232== Process terminating with default action of signal 11 (SIGSEGV)
==232==  Bad permissions for mapped region at address 0x108000
==232==    at 0x108000: ??? (in /workspace/C_Science_Lab/build/ScienceDog)
==232== 
==232== HEAP SUMMARY:
==232==     in use at exit: 0 bytes in 0 blocks
==232==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==232== 
==232== All heap blocks were freed -- no leaks are possible
==232== 
==232== For lists of detected and suppressed errors, rerun with: -s
==232== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
make: *** [Makefile:55: memcheck] Segmentation fault

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 00:27:18