Thread: Problem with compilation

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7

    Unhappy Problem with compilation

    Hello Friends
    I did a c program which i need to submit by 9th
    I compiled it in TurboC2 and its all working fine
    But the task is to be done in GCC.

    This are the commands i used in GCC
    Code:
    gcc QUES2.C -Wall -g -o outpp
    It showed few errors.I corrected them and recompiled
    Then outpp file is formed.Then i used
    Code:
    ./outpp
    This it is Showing as SEGMENTATION ERROR
    I need help in this
    Thanks

    This is the code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    struct snode *check2(struct snode *ptr,int rowc2,int colc2);
    void addToO(struct snode *i1,int rowo,int colo,int co);
    void remO();
    void adj(struct snode *i1);
    int bscore(int rowb,int colb);
    int check(int rowc,int colc);
    int addToC(struct snode *i1);
    void create(int i,int j);
    void path();
    void pathnode();
    struct node
    {
    int row,col;
    struct node *p,*n;
    struct colm *dl;
    };
    struct colm
    {
    int col;
    struct colm *d;
    };
    struct snode
    {
    int row,col,a,b,c;
    struct snode *p,*n,*f;
    };
    struct node *start=NULL,*end=NULL,*head=NULL,*tail=NULL;
    struct colm *temp2=NULL;
    struct snode *openh=NULL,*closeh=NULL,*tempg=NULL,*tempg1=NULL;
    int maxr,nop=0;
    int main()
    {
    int i=1,j=1,ps=0;
    char c;
    FILE *f=NULL;
    f=fopen("map.txt","r");
    while((c=fgetc(f))!=EOF)
    {
    if(c==10)
    {
    i++;
    j=0;
    }
    else if(c=='E' || c=='e')
    {
    start=(struct node *)malloc(sizeof(struct node));
    start->row=i;
    start->col=j;
    }
    else if(c=='X' || c=='x')
    {
    end=(struct node *)malloc(sizeof(struct node));
    end->row=i;
    end->col=j;
    }
    else if(c=='#')
    {
    create(i,j);
    }
    j++;
    }
    maxr=i;
    fclose(f);
    if(start==NULL || end==NULL)
    {
    printf("Wrong Map");
    exit(0);
    }
    path();
    if(nop)
    printf("nopath");
    else
    {
     pathnode();
    f=fopen("map.txt","r");
    i=1;
    j=1;
    while((c=fgetc(f))!=EOF)
    {
    if(check2(tempg1,i,j++))
    {
    ps++;
    printf("P");
    }
    else
    printf("%c",c);
    if(c==10)
    {
    i++;
    j=1;
    }
    }
    }
    fclose(f);
    printf("\n\nNumber of P: %d",ps);
    }
    
    void create(int i,int j)
    {
    struct node *tempn=NULL;
    struct colm *tempc=NULL;
    if(head==NULL)
    {
    tempn=(struct node *)malloc(sizeof(struct node));
    tempn->row=i;
    tempn->col=j;
    tempn->p=NULL;
    tempn->n=NULL;
    tempn->dl=NULL;
    head=tempn;
    tail=tempn;
    }
    else
    {
    if(i==tail->row)
    {
    tempc=(struct colm *)malloc(sizeof(struct colm));
    tempc->col=j;
    tempc->d=NULL;
    if(tail->dl==NULL)
    {
    tail->dl=tempc;
    temp2=tempc;
    }
    else
    {
    temp2->d=tempc;
    temp2=tempc;
    }
    }
    else
    {
    tempn=(struct node *)malloc(sizeof(struct node));
    tempn->row=i;
    tempn->col=j;
    tempn->n=tail->n;
    tail->n=tempn;
    tempn->p=tail;
    tempn->dl=NULL;
    tail=tempn;
    }
    }
    }
    
    int check(int rowc,int colc)
    {
    struct node *t1=NULL;
    struct colm *t2=NULL;
    t1=head;
    while(t1)
    {
     if(t1->row==rowc)
     {
      if(t1->col==colc)
      return 1;
      else if(t1->dl)
       {
       t2=t1->dl;
       while(t2)
        {
        if(t2->col==colc)
        return 1;
        else
        t2=t2->d;
        }
       }
       return 0;
     }
     else
     t1=t1->n;
    }
    return 0;
    }
    
    
    struct snode *check2(struct snode *ptr,int rowc2,int colc2)
    {
    struct snode *temp=NULL;
    temp=ptr;
    while(temp)
    {
    if(temp->row==rowc2 && temp->col==colc2)
    {
    tempg=temp;
    return tempg;
    }
    temp=temp->n;
    }
    return NULL;
    }
    
    
    void path()
    {
    int brk;
    struct snode *i1=NULL;
    i1=(struct snode *)malloc(sizeof(struct snode));
    i1->row=start->row;
    i1->col=start->col;
    i1->a=0;
    i1->b=bscore(i1->row,i1->col);
    i1->c=i1->b;
    i1->f=NULL;
    i1->p=NULL;
    i1->n=NULL;
    openh=i1;
    while(1)
    {
    brk=addToC(i1);
    if(!brk)
    break;
    remO();
    adj(closeh);
    i1=openh;
    if(i1==NULL)
    {
    nop=1;
    break;
    }
    }
    }
    
    
    int addToC(struct snode *i1)
    {
    struct snode *i2=NULL;
    i2=(struct snode *)malloc(sizeof(struct snode));
    i2->row=i1->row;
    i2->col=i1->col;
    i2->a=i1->a;
    i2->b=i1->b;
    i2->c=i1->c;
    i2->f=i1->f;
    if(closeh)
    i2->n=closeh;
    else
    i2->n=NULL;
    i2->p=NULL;
    closeh=i2;
    if(i1->row==end->row && i1->col==end->col)
    return 0;
    else return 1;
    }
    
    
    void remO()
    {
    struct snode *i2=NULL;
    i2=openh;
    openh=openh->n;
    openh->p=NULL;
    free(i2);
    }
    
    
    void adj(struct snode *i1)
    {
    int c9,j1,j2;
    for(j1=-1;j1<=1;j1++)
    for(j2=-1;j2<=1;j2++)
    if((!(j1==0 && j2==0)) && (i1->row+j1>0) && (i1->col+j2>0) && (i1->row+j1<=maxr) && (!check(i1->row+j1,i1->col+j2)) && (!check2(closeh,i1->row+j1,i1->col+j2)))
     {
     c9=i1->a+1+bscore(i1->row+j1,i1->col+j2);
     addToO(i1,i1->row+j1,i1->col+j2,c9);
     }
    }
    
    
    void addToO(struct snode *i1,int rowo,int colo,int co)
    {
    struct snode *i2=NULL,*temp=NULL;
    if(check2(openh,rowo,colo))
    {
     i2=check2(openh,rowo,colo);
     if(i1->a+1<i2->a)
     {
     i2->a=i1->a+1;
     i2->c=i2->a+i2->b;
     i2->f=i1;
     temp=i2->p;
     while(i2->c<temp->c && temp)
     temp=temp->p;
     i2->p->n=i2->n;
     i2->n->p=i2->p;
     i2->p=temp;
     i2->n=temp->n;
     i2->n->p=i2;
     if(temp)
     temp->n=i2;
     else
     openh=i2;
     }
    }
    else
    {
    i2=(struct snode *)malloc(sizeof(struct snode));
    i2->row=rowo;
    i2->col=colo;
    i2->a=i1->a+1;
    i2->b=co-i2->a;
    i2->c=co;
    i2->f=i1;
    i2->n=NULL;
    i2->p=NULL;
    if(openh==NULL)
    openh=i2;
    else
    {
    temp=openh;
    while(i2->c>temp->c && temp->n)
    temp=temp->n;
    if(i2->c>temp->c)
    {
    temp->n=i2;
    i2->p=temp;
    i2->n=NULL;
    }
    else
    {
    if(temp==openh)
    openh=i2;
    i2->n=temp;
    i2->p=temp->p;
    temp->p->n=i2;
    temp->p=i2;
    }
    }
    }
    }
    
    int bscore(int rowb,int colb)
    {
    int x,y;
    x=rowb-end->row;
    y=colb-end->col;
    x=x<0?-x:x;
    y=y<0?-y:y;
    return x+y;
    }
    
    void pathnode()
    {
    struct snode *temp=NULL,*temp1=NULL;
    temp=check2(closeh,end->row,end->col)->f;
    while(temp->n)
    {
    temp1=(struct snode *)malloc(sizeof(struct snode));
    temp1->row=temp->row;
    temp1->col=temp->col;
    temp1->n=tempg1;
    tempg1=temp1;
    temp=temp->f;
    }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Now... get busy and indent that code properly...
    Also get those error messages up where we can see them.

    It's very unlikely anyone is going to weed through 350 lines of sloppy, unindented code to find an unspecified error...

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    I echo what CommonTater said. It's very difficult to read if there is no indentation.


    #2
    Code:
    char c;
    FILE *f=NULL;
    f=fopen("map.txt","r");
    while((c=fgetc(f))!=EOF)
    Read: Definition of EOF and how to use it effectively

    The fgetc function returns an int, not a char. This is very important when comparing against EOF.


    #3
    Don't cast the return value from malloc. The void pointer the function returns can be safely assigned without the need of a cast.



    Other than that I'm not touching this until the formatting/indentation gets addressed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    OK, so you compiled it in debug mode. Now...use the debugger!!!

    Run the program within gdb, and when it crashes, get a backtrace with the bt command so you can figure out why it's crashing.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would help, but not with code looking like that.

    You put the effort in to format it properly first and then I might consider pointing out the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7

    Formated

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    struct snode *check2(struct snode *ptr,int rowc2,int colc2);
    void addToO(struct snode *i1,int rowo,int colo,int co);
    void remO();
    void adj(struct snode *i1);
    int bscore(int rowb,int colb);
    int check(int rowc,int colc);
    int addToC(struct snode *i1);
    void create(int i,int j);
    void path();
    void pathnode();
    
    struct node
    {
    int row,col;
    struct node *p,*n;
    struct colm *dl;
    };
    
    struct colm
    {
    int col;
    struct colm *d;
    };
    
    struct snode
    {
    int row,col,a,b,c;
    struct snode *p,*n,*f;
    };
    
    
    struct node *start=NULL,*end=NULL,*head=NULL,*tail=NULL;
    struct colm *temp2=NULL;
    struct snode *openh=NULL,*closeh=NULL,*tempg=NULL,*tempg1=NULL;
    int maxr,nop=0;
    
    
    int main()
    {
     int i=1,j=1,ps=0;
     char c; 
     FILE *f=NULL;
     f=fopen("map.txt","r");
      while((c=fgetc(f))!=EOF)
       {
        if(c==10)
         {
          i++;
          j=0;
         }
        else if(c=='E' || c=='e')
         {
          start=(struct node *)malloc(sizeof(struct node));
          start->row=i;
          start->col=j;
         }
        else if(c=='X' || c=='x')
         {
          end=(struct node *)malloc(sizeof(struct node));
          end->row=i;
          end->col=j;
         }
        else if(c=='#')
         {
          create(i,j);
         }
        j++;
       }
     maxr=i;
     fclose(f);
      if(start==NULL || end==NULL)
       {
        printf("Wrong Map");
        exit(0);
       }
     path();
     if(nop)
      printf("nopath");
     else
      {
       pathnode();
       f=fopen("map.txt","r");
       i=1;
       j=1;
       while((c=fgetc(f))!=EOF)
        {
         if(check2(tempg1,i,j++))
          {
           ps++;
           printf("P");
          }
         else
          printf("%c",c);
         if(c==10)
          {
           i++;
           j=1;
          }
        }
      }
     fclose(f);
     printf("\n\nNumber of P: %d",ps);
    }
    
    
    void create(int i,int j)
    {
     struct node *tempn=NULL;
     struct colm *tempc=NULL;
     if(head==NULL)
      {
       tempn=(struct node *)malloc(sizeof(struct node));
       tempn->row=i;
       tempn->col=j;
       tempn->p=NULL;
       tempn->n=NULL;
       tempn->dl=NULL;
       head=tempn;
       tail=tempn;
      }
     else
      {
       if(i==tail->row)
        {
         tempc=(struct colm *)malloc(sizeof(struct colm));
         tempc->col=j;
         tempc->d=NULL;
         if(tail->dl==NULL)
          {
           tail->dl=tempc;
           temp2=tempc;
          }
         else
          {
           temp2->d=tempc;
           temp2=tempc;
          }
        }
       else
        {
         tempn=(struct node *)malloc(sizeof(struct node));
         tempn->row=i;
         tempn->col=j;
         tempn->n=tail->n;
         tail->n=tempn;
         tempn->p=tail;
         tempn->dl=NULL;
         tail=tempn;
        }
      }
    }
    
    
    int check(int rowc,int colc)
    {
     struct node *t1=NULL;
     struct colm *t2=NULL;
     t1=head;
     while(t1)
      {
       if(t1->row==rowc)
        {
         if(t1->col==colc)
          return 1;
         else if(t1->dl)
          {
           t2=t1->dl;
           while(t2)
             {
              if(t2->col==colc)
               return 1;
             else
              t2=t2->d;
            }
          }
         return 0;
        }
      else
        t1=t1->n;
      }
     return 0;
    }
    
    
    struct snode *check2(struct snode *ptr,int rowc2,int colc2)
    {
     struct snode *temp=NULL;
     temp=ptr;
      while(temp)
       {
        if(temp->row==rowc2 && temp->col==colc2)
         {
          tempg=temp;
          return tempg;
         }
        temp=temp->n;
       }
     return NULL;
    }
    
    
    void path()
    {
    int brk;
    struct snode *i1=NULL;
    i1=(struct snode *)malloc(sizeof(struct snode));
    i1->row=start->row;
    i1->col=start->col;
    i1->a=0;
    i1->b=bscore(i1->row,i1->col);
    i1->c=i1->b;
    i1->f=NULL;
    i1->p=NULL;
    i1->n=NULL;
    openh=i1;
    while(1)
     {
      brk=addToC(i1);
       if(!brk)
        break;
      remO();
      adj(closeh);
      i1=openh;
      if(i1==NULL)
       {
        nop=1;
        break;
       }
     }
    }
    
    
    int addToC(struct snode *i1)
    {
    struct snode *i2=NULL;
    i2=(struct snode *)malloc(sizeof(struct snode));
    i2->row=i1->row;
    i2->col=i1->col;
    i2->a=i1->a;
    i2->b=i1->b;
    i2->c=i1->c;
    i2->f=i1->f;
    if(closeh)
      i2->n=closeh;
    else
      i2->n=NULL;
    i2->p=NULL;
    closeh=i2;
    if(i1->row==end->row && i1->col==end->col)
      return 0;
    else return 1;
    }
    
    
    void remO()
    {
    struct snode *i2=NULL;
    i2=openh;
    openh=openh->n;
    openh->p=NULL;
    free(i2);
    }
    
    
    void adj(struct snode *i1)
    {
    int c9,j1,j2;
      for(j1=-1;j1<=1;j1++)
        for(j2=-1;j2<=1;j2++)
          if((!(j1==0 && j2==0)) && (i1->row+j1>0) && (i1->col+j2>0) && (i1->row+j1<=maxr) && (!check(i1->row+j1,i1->col+j2)) && (!check2(closeh,i1->row+j1,i1->col+j2)))
            {
              c9=i1->a+1+bscore(i1->row+j1,i1->col+j2);
             addToO(i1,i1->row+j1,i1->col+j2,c9);
            }
    }
    
    
    void addToO(struct snode *i1,int rowo,int colo,int co)
    {
    struct snode *i2=NULL,*temp=NULL;
    if(check2(openh,rowo,colo))
     {
      i2=check2(openh,rowo,colo);
       if(i1->a+1<i2->a)
        {
         i2->a=i1->a+1;
         i2->c=i2->a+i2->b;
         i2->f=i1;
         temp=i2->p;
         while(i2->c<temp->c && temp)
           temp=temp->p;
         i2->p->n=i2->n;
         i2->n->p=i2->p;
         i2->p=temp;
         i2->n=temp->n;
         i2->n->p=i2;
         if(temp)
           temp->n=i2;
         else
           openh=i2;
        }
     }
    else
     {
      i2=(struct snode *)malloc(sizeof(struct snode));
      i2->row=rowo;
      i2->col=colo;
      i2->a=i1->a+1;
      i2->b=co-i2->a;
      i2->c=co;
      i2->f=i1;
      i2->n=NULL;
      i2->p=NULL;
      if(openh==NULL)
        openh=i2;
      else
       {
        temp=openh;
        while(i2->c>temp->c && temp->n)
          temp=temp->n;
        if(i2->c>temp->c)
         {
          temp->n=i2;
          i2->p=temp;
          i2->n=NULL;
         }
        else
         {
          if(temp==openh)
            openh=i2;
          i2->n=temp;
          i2->p=temp->p;
          temp->p->n=i2;
          temp->p=i2;
         }
       }
     }
    }
    
    
    int bscore(int rowb,int colb)
    {
    int x,y;
    x=rowb-end->row;
    y=colb-end->col;
    x=x<0?-x:x;
    y=y<0?-y:y;
    return x+y;
    }
    
    
    void pathnode()
    {
    struct snode *temp=NULL,*temp1=NULL;
    temp=check2(closeh,end->row,end->col)->f;
    while(temp->n)
     {
      temp1=(struct snode *)malloc(sizeof(struct snode));
      temp1->row=temp->row;
      temp1->col=temp->col;
      temp1->n=tempg1;
      tempg1=temp1;
      temp=temp->f;
     }
    }
    Last edited by anudeep2011; 12-09-2011 at 04:44 AM.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7
    Thanks for all for your valuable replies .. I have formatted the code and pasted it again here .. Hoping for replies now

    Quote Originally Posted by hk_mp5kpdw View Post
    #1
    I echo what CommonTater said. It's very difficult to read if there is no indentation.


    #2
    Code:
    char c;
    FILE *f=NULL;
    f=fopen("map.txt","r");
    while((c=fgetc(f))!=EOF)
    Read: Definition of EOF and how to use it effectively

    The fgetc function returns an int, not a char. This is very important when comparing against EOF.


    #3
    Don't cast the return value from malloc. The void pointer the function returns can be safely assigned without the need of a cast.



    Other than that I'm not touching this until the formatting/indentation gets addressed.
    1)How should i change those lines? All i need is to read from a txt file
    2)I have removed the type casting and tried to compile, then it returned an error
    Code:
    error: invalid conversion from ‘void*’ to ‘node*’ [-fpermissive]

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    1)How should i change those lines? All i need is to read from a txt file
    You wrote the rest of this, and you can't read from a text file? Did you follow the link that was provided?

    2)I have removed the type casting and tried to compile, then it returned an error
    It looks like you're not compiling as C. What is the full command line you're using to compile?

  9. #9
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7
    Code:
    gcc QUES2.C -Wall -o try
    Quote Originally Posted by rags_to_riches View Post
    You wrote the rest of this, and you can't read from a text file? Did you follow the link that was provided?


    It looks like you're not compiling as C. What is the full command line you're using to compile?

  10. #10
    Registered User
    Join Date
    Dec 2011
    Location
    Visakhapatnam, India
    Posts
    7
    SOLVED IT
    Thanks to everyone for sparing their time

    Special thanks to rags_to_riches .. I solved it the way you mentioned (using Debugger) Thanks

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    GCC assumes files named as .C (upper case) are C++ files. You can fix that if you care to have it compiled as C code instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation problem
    By zahid990170 in forum C Programming
    Replies: 4
    Last Post: 05-21-2011, 12:07 PM
  2. compilation problem
    By cnu_sree in forum C Programming
    Replies: 6
    Last Post: 10-25-2007, 04:23 AM
  3. Compilation problem
    By Armatura in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2003, 12:42 AM
  4. Compilation problem
    By Skarr in forum Linux Programming
    Replies: 3
    Last Post: 09-27-2002, 12:37 PM
  5. compilation problem
    By Abdi in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 10:59 AM