segmentation fault

This is a discussion on segmentation fault within the C Programming forums, part of the General Programming Boards category; I want to use fopen , fclose , fscanf , fgetc , etc. for about 5000 times in one program. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    segmentation fault

    I want to use fopen , fclose, fscanf , fgetc , etc. for about 5000 times in one program. But when it reaches to 4200, the “segmentation fault” is occurred. What should I do in order to leave this error and run my program completely?
    (I do the program in Linux i386, Suse 8.2 with gcc compiler)

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by yazdan59 View Post
    What should I do in order to leave this error and run my program completely?
    1. Find the error.
    2. Fix the error.
    3. Search for other errors. If you find an error, goto step 2.
    4. Enjoy a nice pizza and a coke.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    There should be no problem doing this in a correct program, there must obviously be some bugs in your code.

    Without seeing the code, it's all just guess-work. There are many 1000's of ways of screwing up which result in seg faults.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    the code

    here is the code. what change i should do in the code? please help me if you can.

    Code:
    #include"stdio.h"
    #include"stdlib.h"
    #define numloop 5000
    #define helixsize 8
    main()
    {
    char hyphen = '!';char atomname = 'H';char u,r,l,d;
    int t=0;int i,j,jj,ii;int RESIDUES,CHAINS,natom;
    int nuatom[2000];int nures[2000];
    char line[200];char name1[2000];char aa[2000];char bb[2000];char dna[2000];char NAME[10];
    
    FILE *out;
    out=fopen("output.c","w");
    FILE *out2;
    out2=fopen("output2.c","w");
    FILE *in1;
    in1=fopen("outnames.c","r");
    
    int no = 0;int fin = 0;int test,num;
    
    for (j=0; j<numloop; j++)
    {
    char q1[43]="/home/Yazdan/dnabase/datadssp/1/";
    char q2[43]="/home/Yazdan/dnabase/datadssp/2/";
    
    fscanf(in1,"%9s\n", &NAME);
    
    strcat(q1,NAME);
    strcat(q2,NAME);
    
    FILE *in;
    
    if ( ( in=fopen(q1,"r") ) != NULL ) { printf("file is available here:%s\n",q1);  in=fopen(q1,"r"); no = no +1;}
    else if ( ( in=fopen(q2,"r") ) != NULL ) { printf("file is available here:%s\n",q2);  in=fopen(q2,"r"); no = no +1;}
    else	{
    	printf("file is not available in sources\n");
    	fin=fin+1;
    	continue;
    	}
    
    for (i=1; i<30; i++)
    {
    if (i==7)	{fscanf(in,"%4d%4d", &RESIDUES,&CHAINS);}
    else	{
    	fgets(line,150,in);
    	fprintf(out,"%s",line);
    	}
    	}
    
    natom =RESIDUES+CHAINS-1;
    
    int n=0;
    for (i=1; i<natom+1; i++)
    {
    fscanf(in,"%4d%3d%2s%2s%3s", &nuatom[i],&nures[i],&name1[i],&aa[i],&dna[i]);
    fgets(line,150,in);
    if(dna[i]==atomname) { n=n+1;}
    }
    
    int length=0;
    int k=0;
    int kk=0;
    
    for (i=1; i<natom+1; i++)
    {
    if( (dna[i]==atomname)|(dna[i+1]==atomname)|(dna[i+2]==atomname)|(dna[i-1]==atomname)|(dna[i-2]==atomname) )
    {
    length=length+1;
    bb[k]=aa[i];
    k=k+1;
    if (length >= helixsize) {
    	if (kk==0) {	for (ii=0; ii<k; ii++)							{
    			if( (t != 0) & (t%75 == 0) ) { fprintf(out2,"\n"); }
    			fprintf(out2," %c",bb[ii]);					                t=t+1;
    			if(ii==k-1){kk=1;}
    			}
    		}
    	else if (kk==1){
    		if( (t != 0) & (t%75 == 0) ) { fprintf(out2,"\n"); }
    		fprintf(out2," %c",bb[k-1]);
    		t=t+1;
    			}
    	}
    }
    else {
    if (length!=0) {printf("length=%d\n",length);}
    length=0;k=0;kk=0;
    }
    }
    	
    fclose(in);
    fprintf(out,"\nno = %d\n",no);
    }/*for number of loops*/
    fclose(in1);
    fclose(out);
    fclose(out2);
    }/*for main*/

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, you're allocating 12,210 bytes on the stack, just in terms of the arrays.... You really shouldn't do that imo....

    Anyway, your indenting is terrible and you're making it extremely hard on yourself and others to actually read your code and figure out what in the world you're trying to do and what you're actually doing.

    Code:
    if ( ( in=fopen(q1,"r") ) != NULL )
    { printf("file is available here:&#37;s\n",q1);  in=fopen(q1,"r"); no = no +1;}
    You open a file, check if it's successful, and then open it again. Wastes of memory and resources.

    Code:
    else if ( ( in=fopen(q2,"r") ) != NULL )
    { printf("file is available here:%s\n",q2);  in=fopen(q2,"r"); no = no +1;}
    Same thing as before.

    Also, you're never closing in until the end, which means you only close the last file opened with in inside the loop. Fix the resource leak.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    description

    Quote Originally Posted by MacGyver View Post
    You open a file, check if it's successful, and then open it again. Wastes of memory and resources.

    Also, you're never closing in until the end, which means you only close the last file opened with in inside the loop. Fix the resource leak.
    there are 5000 files which may locate in two different folders. Note that they may be located. it may not be available. So, first file should check if there is in source directories or not. Then do some work on it. we can not close that file at the end of if section because it is needed to read from that file. in this case, how can i run it successfully? if there is a better way, please let me know.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    hi!
    Code:
    Originally Posted by MacGyver  
    You open a file, check if it's successful, and then open it again. Wastes of memory and resources.
    
    Also, you're never closing in until the end, which means you only close the last file opened with in inside the loop. Fix the resource leak.
    Code:
    there are 5000 files which may locate in two different folders. Note that they may be located. it may not be available. So, first file should check if there is in source directories or not. Then do some work on it. we can not close that file at the end of if section because it is needed to read from that file. in this case, how can i run it successfully? if there is a better way, please let me know.
    1.Agreed that they may be located and not available .
    The very act of fopen being succesful(i.e. no NULL value) implies the file has been LOCATED,OPENED AND THE CORRESPONDING FILE POINTER IS POINTING TO THE FILE STRUCTURE.
    So no need to open it twice .
    You are basically checking the availability of either of files else performing the
    requisite operations.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    2.
    Code:
    for (j=0; j<numloop; j++)
    {
    char q1[43]="/home/Yazdan/dnabase/datadssp/1/";
    char q2[43]="/home/Yazdan/dnabase/datadssp/2/";
    numloop =5000 if q1[43],q2[43]
    need to be initialized with same string always they may be done outside
    loop.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    Code:
    #define numloop 5000
    Code:
    char aa[2000];char bb[2000];char dna[2000];

    Code:
    for (j=0; j<numloop; j++)
    Code:
    {fscanf(in,"%4d%4d", &RESIDUES,&CHAINS);}
    Code:
    natom =RESIDUES+CHAINS-1;
    Code:
    for (i=1; i<natom+1; i++)
    Code:
    bb[k]=aa[i];

    numloop is 5000 but size of arrays is 2000
    the 'natom=residues+chains-1' loop is user input dependent.
    Check if you are not exceeding the limits in any cases.
    This may be a cause of segmentation fault

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    As mentioned - fix the indentation.

    Or use an indent program to make a start, then keep it clean.
    Code:
    #include"stdio.h"
    #include"stdlib.h" /*!! Use <> for system includes */
    #include <string.h>
    #define numloop 5000
    #define helixsize 8
    int main()
    {
        char hyphen = '!';
        char atomname = 'H';
        char u, r, l, d;
        int t = 0;
        int i, j, jj, ii;
        int RESIDUES, CHAINS, natom;
        int nuatom[2000];/*!! Consider putting these 6 variables into a struct */
        int nures[2000];/*!! Also, make 2000 another #define constant */
        char line[200];
        char name1[2000];
        char aa[2000];
        char bb[2000];
        char dna[2000];
        char NAME[10];
    
        FILE *out;
        out = fopen("output.c", "w");
        FILE *out2;
        out2 = fopen("output2.c", "w");
        FILE *in1;
        in1 = fopen("outnames.c", "r");
    
        int no = 0;
        int fin = 0;
        int test, num;
    
        for (j = 0; j < numloop; j++) {
            char q1[43] = "/home/Yazdan/dnabase/datadssp/1/";
            char q2[43] = "/home/Yazdan/dnabase/datadssp/2/";
    
            fscanf(in1, "%9s\n", &NAME); /*!! No & required, NAME is an array of char already */
    
            strcat(q1, NAME);
            strcat(q2, NAME);
    
            FILE *in;
    
            if ((in = fopen(q1, "r")) != NULL) {
                printf("file is available here:%s\n", q1);
                in = fopen(q1, "r"); /*!! it's already open, trying again won't help */
                no = no + 1;
            } else if ((in = fopen(q2, "r")) != NULL) {
                printf("file is available here:%s\n", q2);
                in = fopen(q2, "r");
                no = no + 1;
            } else {
                printf("file is not available in sources\n");
                fin = fin + 1;
                continue;
            }
    
            for (i = 1; i < 30; i++) {
                if (i == 7) {
                    fscanf(in, "%4d%4d", &RESIDUES, &CHAINS);
                } else {
                    fgets(line, 150, in); /*!! in place of "150", use  "sizeof(line)"  */
                    fprintf(out, "%s", line);
                }
            }
    
            natom = RESIDUES + CHAINS - 1;
            /*!! Add a diagnostic check to make sure it's less than 2000-1 */
    
            int n = 0;
            for (i = 1; i < natom + 1; i++) {
                /*!! I would suggest fgets() the whole line to begin with, then sscanf on that line */
                /*!! rather than fscanf + some random fgets() to remove trailing data. */
                fscanf(in, "%4d%3d%2s%2s%3s", &nuatom[i], &nures[i], &name1[i], &aa[i], &dna[i]);
                fgets(line, 150, in);
                if (dna[i] == atomname) {
                    n = n + 1;
                }
            }
    
            int length = 0;
            int k = 0;
            int kk = 0;
    
            /*!! consider making this a separate function to stop main getting too long */
            for (i = 1; i < natom + 1; i++) {
                /*!! you probably want || here, not | */
                if ((dna[i] == atomname) | (dna[i + 1] == atomname) | (dna[i + 2] == atomname) |
                    (dna[i - 1] == atomname) | (dna[i - 2] == atomname)) {
                    length = length + 1;
                    bb[k] = aa[i];
                    k = k + 1;
                    if (length >= helixsize) {
                        if (kk == 0) {
                            for (ii = 0; ii < k; ii++) {
                                if ((t != 0) & (t % 75 == 0)) {
                                    fprintf(out2, "\n");
                                }
                                fprintf(out2, " %c", bb[ii]);
                                t = t + 1;
                                if (ii == k - 1) {
                                    kk = 1;
                                }
                            }
                        } else if (kk == 1) {
                            if ((t != 0) & (t % 75 == 0)) {
                                fprintf(out2, "\n");
                            }
                            fprintf(out2, " %c", bb[k - 1]);
                            t = t + 1;
                        }
                    }
                } else {
                    if (length != 0) {
                        printf("length=%d\n", length);
                    }
                    length = 0;
                    k = 0;
                    kk = 0;
                }
            }
    
            fclose(in);
            fprintf(out, "\nno = %d\n", no);
        }                           /*for number of loops */
        fclose(in1);
        fclose(out);
        fclose(out2);
        return 0;
    }                               /*for main */
    
    
    Then fix these warnings
    $ gcc -std=c99 -Wall foo.c
    foo.c:6: warning: return type defaults to `int'
    foo.c: In function `main':
    foo.c:37: warning: char format, different type arg (arg 3)
    foo.c:39: warning: implicit declaration of function `strcat'
    foo.c:7: warning: unused variable `hyphen'
    foo.c:9: warning: unused variable `u'
    foo.c:9: warning: unused variable `r'
    foo.c:9: warning: unused variable `l'
    foo.c:9: warning: unused variable `d'
    foo.c:11: warning: unused variable `jj'
    foo.c:31: warning: unused variable `test'
    foo.c:31: warning: unused variable `num'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    Thanks

    Quote Originally Posted by Salem View Post
    Code:
    if ((in = fopen(q1, "r")) != NULL) {
                printf("file is available here:%s\n", q1);
                in = fopen(q1, "r"); /*!! it's already open, trying again won't help */
                no = no + 1;
            } else if ((in = fopen(q2, "r")) != NULL) {
                printf("file is available here:%s\n", q2);
                in = fopen(q2, "r");            no = no + 1;
            } else {
                printf("file is not available in sources\n");
                fin = fin + 1;
                continue;
            }
    Thanks for all of your suggestions particularly Salem. I only deleted these two section(with color above) and every thing works properly.
    Your comments were very useful.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fscanf(in1,"&#37;9s\n", &NAME);

    NAME is already a pointer (array name is a pointer to the first element)
    no need to use &
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21