Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi..I have written a C code for that performs well for a 1602X13 size text file. But for 85430X13 size file it is giving Segmentation fault (core dumped) error. I am using gcc compiler and Turbo C both. I am giving the code here for turbo c compiler.

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

void main()
{
    int i=0,j,k,x,count=0,n=0,m=13,p=0,q=13;
    float s=0.0,dis=0.0;
    float * avg;
    float ** a;
    float ** b;
    char buf[BUFSIZ];
    FILE *fp1,*fp2,*fp3,*fp4;
    clrscr();
    fp1=fopen("a1.txt","r");
    fp2=fopen("a2.txt","r");
    fp3=fopen("result.txt","wb");
    while (fgets(buf,sizeof(buf),fp1) != NULL)
    n++; // counting rows of a1.txt

    while (fgets(buf,sizeof(buf),fp2) != NULL)
    p++;    // counting rows of a2.txt
    rewind(fp1);

    for(i=0;i<n;i++)
    {    a[i] = (float*) malloc(n * m * sizeof(float));
        for(j=0;j<m;j++)
        {

            fscanf(fp1,"%f",&a[i][j]);
        }
           printf("\n");
    }
    printf("\n");
    rewind(fp2);

    for(i=0;i<p;i++)
    {      b[i] = (float *) malloc(p * q * sizeof(float));
        for(j=0;j<q;j++)
        {
            fscanf(fp2,"%f",&b[i][j]);
        }
        printf("\n");
    }
          printf("\n");
    //calculation part
    avg = (float *) malloc(n * sizeof(float));
     for(x=0;x<=(n-1);x++)
     {
     if(count!=(n-p)+1){
        i=x;k=0;
        while(i<(p+x))
        {
            for(j=0;j<q;j++)
            {
                s=s+pow((a[i][j]-b[k][j]),2.0);

            }
                dis=dis+sqrt(s);


            s=0;
            i++;
            k++;

        }
        count++;
            avg[i]=dis;
            fprintf(fp3,"%f\n",avg[i]);
        dis=0;
     }
     else
     break;
    }

    getch();
    fclose(fp1);
    fclose(fp2);
    fclose(fp4);
    fclose(fp3);
    free(a);
    free(b);
    free(avg);
}


Posted
Updated 30-Aug-13 1:10am
v2
Comments
[no name] 30-Aug-13 7:28am    
Have you tried using the debugger to find out where this is occurring?
Member 10227515 30-Aug-13 9:51am    
no, I also want some help for compiling C code in gcc. I have tried it first time.
[no name] 30-Aug-13 19:45pm    
It seems you didn't write this and you have made no effort to find the problem yourself. Learn how to use the debugger and then you can debug the code yourself.
Set a breakpoint at the line
avg = (float *) malloc(n * sizeof(float));
If the code runs to there then you know the problem is after that line. If not it is before. By moving the breakpoint you can locate where the problem is occurring. After that it should be easy.
Member 10227515 31-Aug-13 2:49am    
Thats not true...i have write this code. I said, i haven't tried debugging on gcc but on Turbo C i did. the problem is in the line u mentioned...
avg = (float *) malloc(n * sizeof(float));
May be i am not allocating enough memory or I am not allocating it in a right way.
[no name] 31-Aug-13 3:29am    
Well if you know what line then you know the next step - what is n at this point?

You have not provided enough in your question for anyone but you to solve this.

As for my earlier comments about authorship I based my conclusion on your answer to this question from FB "What is BUFSIZ set to?". That was a very odd answer from someone who wrote the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900