Like Tree3Likes
  • 3 Post By anduril462

Strange Segmentation Fault

This is a discussion on Strange Segmentation Fault within the C Programming forums, part of the General Programming Boards category; I'm working on a program that takes, as input, a file written in C and converts it to IA-32 assembly. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Strange Segmentation Fault

    I'm working on a program that takes, as input, a file written in C and converts it to IA-32 assembly. If the inp pointer is NULL, it does what it needs to do. If it isn't, it displays Segmentation Fault and exits. However, it still creates the other files that are needed but does not write text to them.

    Segmentation Faults are difficult to trace. I'd be extremely grateful for any help rendered.

    Thank you

    Code:
    int main(int argc,char *argv[]) 
    { 
    int valid=0,k=0,l=0,i,x,y,z; 
    FILE *inp, *outp, *tpin, *tpout; 
    char ch, temp[SIZE], decl[SIZE]; 
    nodetype *list,*newnode,*prev; 
    prev=NULL; 
    //fflush(outp); 
     
    intro("This program takes a program written in ‘C’ and converts it into a program written in assembly language."); 
     
    //valid=readC(inp,argv[1]); 
    //if(valid==1) 
    //DEBUG 
     
    inp=fopen(argv[1],"r"); 
     
    if(inp!=NULL) 
        { 
        
        outp=fopen("asmcode.txt","w");
    
        fprintf(outp,".file\t\"main.c\"\n\t.section\t.rodata\n");
    
        tpin=fopen("declarein.txt","w");            //Stores input placeholder information
    
        tpout=fopen("declareout.txt","w");            //Stores output placeholder information
    
        while(!feof(inp))                    //grabs a line from the input file 
            {
    
            //do{ 
            fgets(temp,SIZE,inp); 
            //temp[i]=ch; 
            //i++; 
            //}while(ch!='\n'); 
            //temp[i]='\0';
    
            printf("jjj\n");
     
            if((temp[0]=='s')&&(temp[1]=='c')&&(temp[2]=='a')&&(temp[3]=='n')&&(temp[4]=='f')&&(temp[5]=='(')) 
                {
    
     
                for(x=6,y=0;x<10,y<4;x++,y++) 
                    decl[y]=temp[x]; 
                fprintf(outp,".LC%d:\n\t.string\t",l);    //Stores input assembly declarations in main output file 
                fprintf(tpin,"%s",decl);        //Keeps a copy of the placeholder in declarein.txt 
                fputs("\n",tpin); 
                l++; 
                for(z=0;z<4;z++) 
                    fprintf(outp,"%s\n\t.text\n",decl[z]); 
                } 
            else if((temp[0]=='p')&&(temp[1]=='r')&&(temp[2]=='i')&&(temp[3]=='n')&&(temp[4]=='t')&&(temp[5]=='f')&&(temp[6]=='(')) 
                {
    
                for(x=7,y=0;x!='\0';x++,y++) 
                    decl[y]=temp[x]; 
                decl[y]='\0'; 
                fprintf(outp,".LC%d:\n\t.string\t",k);    //Stores output assembly declarations in main output file 
                fprintf(tpout,"%s",decl);        //Keeps a copy of the placeholder in declarein.txt 
                fputs("\n",tpout); 
                k++; 
                for(z=0;z<4;z++) 
                    fprintf(outp,"%s\n\t.text\n",decl[z]); 
                } 
            else for(x=0;x!='\n';x++) 
                        if(temp[x]=='=') 
                            {     
                                newnode=(nodetype*)malloc(sizeof(nodetype)); 
                                newnode->next=list; 
                                list=newnode; 
                                for(y=0,z=0;y<x;y++,z++) 
                                newnode->nm[z]=temp[y]; 
                                newnode->val=temp[x++]; 
                            } 
        } 
        
        fclose(tpin); 
        fclose(tpout); 
        fprintf(outp,".globl\tmain\n\t.type\tmain@function\nmain:\n\tpushl\t%cebp\n\tmov\t%cesp,%cebp\n",'%','%','%'); 
        fseek(inp,0L,0); 
        while(!feof(inp)) 
            { 
            fgets(temp,SIZE,inp); 
            if((temp=="int main()")||(temp[0]=='#')||(temp[0]==' ')||(temp[0]=='{')||(temp[0]=='}')) 
                fseek(inp,1L,1); 
            //convert(inp,outp,list); 
            fseek(inp,1L,1); 
            } 
        fprintf(outp,"\n\tmov\t$0,%ceax\n\tleave\n\tret\n\t.size\tmain,.-main\n\t.ident\t\"GCC:(Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2\"\n\t.section\t.note.GNU-stack,\"\",@progbits",'%'); 
        } 
    else { 
        printf("\n\nInvalid input file"); 
        exit(1); 
        } 
    fclose(outp); 
    fclose(inp); 
    terminate(); 
    return 0; 
    }
    Last edited by gw247; 11-03-2011 at 12:40 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,443
    Seg faults are not terribly difficult to trace if you know how to use a debugger. Learn to use one, it will save you hours of headache.

    Your code is rife with potential seg faults and other errors and bad practices. Not to mention, we would need the full, compilable code to iron out all your issues. But what you've provided has enough problems to keep you busy for a while:

    • You don't check if argv[1] is valid (or if argc > 1) before using it
    • You open 4 files, but only check if one file pointer is null before use
    • You malloc memory, but don't check if malloc succeeded (or failed, returning null) before using that memory
    • You didn't provide us with the definition of nodetype, so for all we know, your loops that mess with newnode->nm[z] have overflows.
    • You control loops with feof. Read this: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    • You use horrible variable names like k, l, i, x, y, z, and don't comment your code well, so we have no idea what you're trying to do and can't check for logic errors.
    • You compare strings with == (line 86) instead of strcmp. You compare sequences of characters with a giant list of char by char ==. Try using strncmp or memcmp for this.
    • You never initialize list to null, so the list you build is never properly terminated.
    • Your loop on line 51 probably doesn't do what you think it does. It prints decl, then decl starting at the 2nd char, then again starting at the 3rd, and finally starting at the 4th.
    • Your loop on line 57 does not check if you overrun the bounds of decl or temp



    That's what I found in my 5 minute overview. You have some serious work to do improving your C skills. Perhaps you should put this project aside for the time being and go back to the basics.

    Read some tutorials (like ours here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy) and Google for more. Crack open those textbooks and class notes. Work the practice problems. Then, get paper and pencil, and plan your solution to this problem thoroughly before touching the keyboard. Once it's planned, code in very small sections, around 5-10 lines, and test as you go, making sure everything works perfectly before moving on to the next part.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Quote Originally Posted by anduril462 View Post
    Seg faults are not terribly difficult to trace if you know how to use a debugger. Learn to use one, it will save you hours of headache.
    I'm gonna go through my basics first

    Quote Originally Posted by anduril462 View Post

    • You don't check if argv[1] is valid (or if argc > 1) before using it
    • You open 4 files, but only check if one file pointer is null before use
    • You malloc memory, but don't check if malloc succeeded (or failed, returning null) before using that memory
    • You control loops with feof. Read this: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    • You use horrible variable names like k, l, i, x, y, z, and don't comment your code well, so we have no idea what you're trying to do and can't check for logic errors.
    • You compare strings with == (line 86) instead of strcmp. You compare sequences of characters with a giant list of char by char ==. Try using strncmp or memcmp for this.
    • You never initialize list to null, so the list you build is never properly terminated.
    • Your loop on line 51 probably doesn't do what you think it does. It prints decl, then decl starting at the 2nd char, then again starting at the 3rd, and finally starting at the 4th.
    • Your loop on line 57 does not check if you overrun the bounds of decl or temp
    Checked and solved. Thank you.

    I appreciate you taking the time to go through my code. This was a real eye-opener. I'm gonna start solidifying my basics. Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  2. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:19 AM
  3. strange segmentation fault
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-19-2004, 08:46 AM
  4. strange segmentation fault error!
    By jayjay in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2003, 03:25 PM

Tags for this Thread


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