简体   繁体   English

实现 malloc 时出现分段错误

[英]Segmentation fault in implementing malloc

I am coding a gauss seidel program and I am currently having a problem in using malloc.我正在编写 gauss seidel 程序,目前在使用 malloc 时遇到问题。

Please help me with this.请帮我解决一下这个。 I haven't started with the gauss seidel iterative because I was stuck here.我还没有开始使用高斯赛德尔迭代,因为我被困在这里。

The error says "segmentation fault (core dumped)."错误显示“分段错误(核心已转储)”。 I do not know what it means.我不知道这是什么意思。 I tried scanning the code several times but I just can't find the error.我尝试多次扫描代码,但我找不到错误。

COULD THERE BE SOMETHING WRONG WITH THE WAY I USED FSCANF?我使用 FSCANF 的方式可能有问题吗?

#include <stdio.h>
#include <stdlib.h>

//structure declarations
struct table2   {
int k;
float value;
int row;
int col;
int nextK_row;
int nextK_col;
};

struct table3   {
int index;
int fir;
int fic;
};


//function prototypes
void allocate_memory(int num_unknowns, int num_entries, struct table2 *pTable2, struct table3  *pTable3, float *b, FILE* fInput, FILE* fOutput);

void free_close (FILE* fInput, FILE* fOutput, struct table2 *pTable2, struct table3 *pTable3, float *b);


int main (int argc, char* argv[])   {


//variable declarations
FILE *fInput=NULL;
FILE *fOutput=NULL;
struct table2 *pTable2=NULL;
struct table3 *pTable3=NULL;
int num_unknowns, num_entries;
float *b=NULL;
int i, j, m;
int count1, count2, count3, count4, l;



//check if arguments from user is 3, else error
if (argc!=3)    {
    printf("Error, the number of arguments should be exactly as needed(three).\n");
    return 1;
}


//open input file
fInput = fopen(argv[1], "r");
//check if input file opened successfully
if (fInput == NULL) {
    printf("Error. Input file wasn't opened successfully.\n");
    return 1;
}

//open output file
fOutput = fopen(argv[2], "w");
//check if output file opened successfully
if (fOutput == NULL)    {
    printf("Error. Output file wasn't opened successfully.\n");
    return 1;
}

//scan no. of unknowns from file and check if successful
count1 = fscanf(fInput, " %d", &num_unknowns);
if (count1 != 1)    {
    printf("Error, fscanf() did not read number of unknowns successfully.");    
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}


//scan no. of entries from file and check if successful
count2 = fscanf(fInput, " %d", &num_entries);
if (count2 != 1)    {
    printf("Error, fscanf() did not read number of entries successfully."); 
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}


//call function to allocate memory
allocate_memory(num_unknowns, num_entries, pTable2, pTable3, b, fInput, fOutput);


//loop to read values from Table2 and check if it read successfully
for(i=1; i<=num_entries; i++)   {
    count3 = fscanf(fInput, " %d %f %d %d %d %d", &(pTable2[i].k), &(pTable2[i].value), &(pTable2[i].row), &(pTable2[i]).col, &(pTable2[i].nextK_row), &(pTable2[i].nextK_col));
    if (count3 != 6)    {
        printf("Error, fscanf() did not read table 2 values successfully.");
//call function to free memory and close files
        free_close (fInput, fOutput, pTable2, pTable3, b);
        return 1;
    }
}

//loop to get values from Table3
for(j=1; j<=num_unknowns; j++)  {
    count4 = fscanf(fInput, " %d %d %d", &(pTable3[j].index), &(pTable3[j].fir), &(pTable3[j].fic));
    if (count4 != 3)    {
        printf("Error, fscanf() did not read table3 values successfully.");
//call function to free memory and close files
        free_close (fInput, fOutput, pTable2, pTable3, b);
        return 1;
    }
}

//loop to get constants from file
for(m=1; m<=num_unknowns ; m++) {
    l = fscanf(fInput, " %f", &(b[m]));
    if (l != 1) {
        printf("Error, fscanf() did not read constant values successfully.");
//call function to free memory and close files
        free_close (fInput, fOutput, pTable2, pTable3, b);
        return 1;
    }
}



//test print
for(i=1; i<=num_entries; i++)   {
    printf("%d\t", pTable2[i].k);
    printf("%f\t", pTable2[i].value);
    printf("%d\t", pTable2[i].row);
    printf("%d\t", pTable2[i].col);
    printf("%d\t", pTable2[i].nextK_row);
    printf("%d\t", pTable2[i].nextK_col);
}
printf("\n\n\n");
for(j=1; j<=num_unknowns; j++)  {
    printf("%d\t", pTable3[j].index);
    printf("%d\t", pTable3[j].fir);
    printf("%d\t", pTable3[j].fic);
}
printf("\n\n\n");
for(m=1; m<=num_unknowns ; m++) {
    printf("%f", b[m]);
    }
printf("\n\n\n");







//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);


return 0;
}



//function to allocate memory
void allocate_memory(int num_unknowns, int num_entries, struct table2* pTable2, struct table3* pTable3, float* b, FILE* fInput, FILE* fOutput)  {


//allocate memory for table 2   
pTable2 = (struct table2* ) malloc(24*num_entries);
if (pTable2 == NULL)    {
    printf("Error, memory allocation for table2 failed.");
//call function to free memory and close files
    free_close (fInput, fOutput, pTable2, pTable3, b);
    exit(-1);
}

//allocate memory for table 3
pTable3 = (struct table3* ) malloc(12*num_unknowns);
if (pTable3 == NULL)    {
    printf("Error, memory allocation for table3 failed.");
//call function to free memory and close files
    free_close (fInput, fOutput, pTable2, pTable3, b);
    exit(-1);
} 

//allocate memory for constants
b = (float*) malloc(sizeof(float)*num_unknowns);
if (b == NULL)  {
    printf("Error, memory allocation for matrix B entries failed.");
//call function to free memory and close files
    free_close (fInput, fOutput, pTable2, pTable3, b);
    exit(-1);
}

return;
}


//function to free allocated memory and close files

void free_close (FILE* fInput, FILE* fOutput, struct table2 *pTable2, struct table3 *pTable3, float *b) {

if (fInput != NULL)
    fclose(fInput);
if (fOutput != NULL)
    fclose(fOutput);

if (pTable2 != NULL)
    free(pTable2);
if (pTable3 != NULL)
    free(pTable3);
if (b != NULL)
    free(b);

return;
}

In this function call:在这个函数调用中:

allocate_memory(num_unknowns, num_entries, pTable2, pTable3, b, fInput, fOutput);

you pass all the variables by value.您按值传递所有变量。 Then inside allocate_memory you change the local copies of those variables.然后在allocate_memory更改这些变量的本地副本。 Those changes do not affect the variables in main() .这些更改不会影响main()的变量。

Your segfaults probably come from doing ptable2[i] etc. in main() because pTable2 is still NULL .您的段错误可能来自在main()执行ptable2[i]等,因为pTable2仍然是NULL

To fix this , pass the variables by reference.要解决此问题,请通过引用传递变量。 However , your whole free_close setup is pretty ugly.但是,您的整个free_close设置非常丑陋。 I'd suggest putting all the relevant control variables into a struct ;我建议将所有相关的控制变量放入一个struct and have main() call a function which has the rest of your code in it, and main() can do the freeing after that function ends.并让main()调用一个包含其余代码的函数, main()可以在该函数结束后进行释放。

Also you should learn how to figure out where a segmentation fault is occurring, it is a useful skill.您还应该学习如何找出发生分段错误的位置,这是一项有用的技能。 If you don't have a debugger set up and don't want to learn it just now, you can "debug" by inserting output statements (including a flush) in your code and running the program and seeing if that output appeared.如果您没有设置调试器并且现在不想学习它,您可以通过在代码中插入输出语句(包括刷新)并运行程序并查看是否出现该输出来“调试”。 That way, you can gradually narrow it down to which line has the problem.这样,您可以逐渐缩小到哪一行有问题。

Instead of pTable2 = (struct table2* ) malloc(24*num_entries);而不是pTable2 = (struct table2* ) malloc(24*num_entries); why not pTable2 = (struct table2* ) malloc(sizeof (struct table2)*num_entries);为什么不pTable2 = (struct table2* ) malloc(sizeof (struct table2)*num_entries);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM