Thread: C Noob: Exe crashes, been stuck on it for a few days, can't figure out why.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    8

    C Noob: Exe crashes, been stuck on it for a few days, can't figure out why.

    The idea is to build a linked geometric shaped in memory that will read a set of numbers.The first option works fine.For the second and third it crashes when I try to null the values of node,even if I ommit that piece of code, it still crashes after that. If it helps the shape for the second option (triangle) is ImageShack® - Online Photo and Video Hosting . For the third one (half) it's basically just the right half of the increasing boxes with every row, so it's basically identical. Any help would be appreciated and I'm sure someone will ask, yes this is coursework.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct nodedef{
                   struct nodedef *ptrL;
                   struct nodedef *ptrD;
                   struct nodedef *ptrU;
                   struct nodedef *ptrR;
                   int numb;
                   };
    
    typedef struct nodedef node;
    
    void null_node(node *);
    node *gridlist(void);
    node *triangle(void);
    node *half(void);
                   
    main()
          {
          int choice=0,k;
          
          do{
             printf("\nMenu\n\n");
             printf("1. Option 1 : Grid\n");
             printf("2. Option 2 : Triangle\n");
             printf("3. Option 3 : Half grid\n");
             printf("9. Exit\n\n");    
          
          
          
          do{
             fflush(stdin);
             k=scanf("%d", &choice);
             if(k!=1){ printf("\nInvalid input.\n") ;
             }
             }while(k!=1);
             
             switch(choice){
                            case 1:printf("Placeholder 1");
                            gridlist();
                            break;
                            case 2:printf("Placeholder 2");
                            triangle();
                            break;
                            case 3:printf("Placeholder 3");
                            half();
                            break;
                            case 9:printf("Stopping program.\n");
                            getch();
                            return 0;
                            default: printf("Please choose again.");
    }
    
    }while(choice!=9);
    }
             
    node *gridlist(void){
    
         node *start, *currV, *prevV, *currH, *prevH, *next;
         int i,j,columns,rows;
         
         printf("Please specify how many rows and colums you'd like in the Grid option");
         scanf("%d%d",&rows,&columns);
        
         start=(node *)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         currH=start;
         prevH=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         
         //creating the first row first
         for(i=0;i<columns-1;i++){
                                currH->ptrR=(node *)malloc(sizeof(node));
                                prevH=currH;
                                currH=currH->ptrR;
                                null_node(currH);
                                currH->ptrL=prevH;
                                printf("Please type in your number\n");
                                scanf("%d", &currH->numb);
                                }
         //creating the rest of the structure one pointer that stays one row behind the current horizontal traversing through the rows;
         printf("\nTEST\n");
         for(i=0;i<rows-1;i++){
                               next=currV;
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV); 
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               currH=currV;
                               prevH=currV;
                               printf("\nTEST\n");
                               for(j=0;j<columns-1;j++){
                                                      currH->ptrR=(node *)malloc(sizeof(node));
                                                      prevH=currH;
                                                      currH=currH->ptrR;
                                                      null_node(currH);
                                                      currH->ptrL=prevH;
                                                      next=next->ptrR;
                                                      next->ptrD=currH;
                                                      currH->ptrU=next;  
                                                      printf("Please type in your number\n");
                                                      scanf("%d", &currH->numb);                       
    }
    }     
    
         
         printf("\nCurrH %p %p %p %d\n",currH,currH->ptrR,currH->ptrL,currH->numb); 
         printf("\nCurrV %p %p %d\n",currV,currV->ptrL,currV->numb);               
    return;             
    }
    
    void null_node(node *curr){
    
    
    
      curr->ptrL=NULL;
      curr->ptrU=NULL;
      curr->ptrD=NULL;
      curr->ptrR=NULL;
      
      return;
    }
    
    node *triangle(void){
         
         node *start, *currH, *prevH, *prevV, *currV, *next;
         int i,j,k,rowcount=1,rows;
         printf("Please specify how many rows you'd like in the Triangle option\n");
         scanf("%d",&rows);
         
         start=(node*)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         for(i=0;i<rows-1;i++){
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV);
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               next=prevV;
                               printf("\nTEST\n");
         //left side loop
                               for(j=0;j<rowcount;j++){
                                                       printf("\nTEST2\n");
                                                       currH=currV;
                                                       prevH=currV;
                                                       printf("\nTEST2\n");
                                                       currH->ptrL=(node *)(sizeof(node));
                                                       printf("\nTEST2\n");
                                                       prevH=currH;
                                                       currH=currH->ptrL;
                                                       printf("\nTEST3\n");
                                                       currH->ptrL=NULL; //crashes here
                                                       currH->ptrU=NULL;
                                                       currH->ptrD=NULL;
                                                       currH->ptrR=NULL;
                                                       printf("\nTEST5\n");
                                                       currH->ptrR=prevH;
                                                       next=next->ptrL;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("\nTEST3\n");
                                                       printf("Please type in your number\n");
                                                       scanf("%d", &currH->numb);                                                   
                                                       }
     //resetting next to prevV for the right side loop
     //right side loop;                          
                                                 printf("TEST\n");
                               next=prevV;
                               for(k=0;k<rowcount;k++){
                                                       currH=currV;
                                                       prevH=currV;
                                                       currH->ptrR=(node *)(sizeof(node));
                                                       prevH=currH;
                                                       currH=currH->ptrR;
                                                       null_node(currH);
                                                       currH->ptrL=prevH;
                                                       next=next->ptrR;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("Please type in your number\n");            
                                                       scanf("%d", &currH->numb);
                                                       }
                                                       
                               rowcount++;
                                                      
         }
         
         }
    
    
    node *half(void){
         node *start, *currH, *prevH, *prevV, *currV, *next;
         int i,j,k,rowcount=1,rows;
         
         printf("Please specify how many rows you'd like in the Triangle option\n");
         scanf("%d",&rows);
         
         start=(node*)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         for(i=0;i<rows-1;i++){
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV);
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               next=prevV;
                               printf("\nTEST\n");
    //right side loop                           
                                                for(k=0;k<rowcount;k++){
                                                       currH=currV;
                                                       prevH=currV;
                                                       currH->ptrR=(node *)(sizeof(node));
                                                       prevH=currH;
                                                       currH=currH->ptrR;
                                                       null_node(currH);
                                                       currH->ptrL=prevH;
                                                       next=next->ptrR;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("Please type in your number\n");            
                                                       scanf("%d", &currH->numb);
                                                       }                 
                               rowcount++;
                               
                               
    }
         
         
         
         
         }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to find an indentation style you like and use it consistently. Next you need to configure your project to emit warnings. Here are the warnings and errors I get when I try to compile your program:
    main.c|19|error: return type defaults to ‘int’|
    main.c||In function ‘main’:|
    main.c|50|error: implicit declaration of function ‘getch’|
    main.c||In function ‘gridlist’:|
    main.c|114|warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘struct node *’|
    main.c|114|warning: format ‘%p’ expects type ‘void *’, but argument 3 has type ‘struct nodedef *’|
    main.c|114|warning: format ‘%p’ expects type ‘void *’, but argument 4 has type ‘struct nodedef *’|
    main.c|115|warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘struct node *’|
    main.c|115|warning: format ‘%p’ expects type ‘void *’, but argument 3 has type ‘struct nodedef *’|
    main.c|116|error: ‘return’ with no value, in function returning non-void|
    main.c||In function ‘half’:|
    main.c|206|warning: unused variable ‘j’|
    main.c|250|warning: control reaches end of non-void function|
    main.c||In function ‘triangle’:|
    main.c|201|warning: control reaches end of non-void function|
    ||=== Build finished: 3 errors, 8 warnings ===|
    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Thanks for the quick reply Jim.Compiling works fine for me using Bloodshed Dev C++, however I'll take your advice into account.Anyway I think I found the reason why my exe crashes.It appears to be a segmentation fault,which occurs because I was trying to use a null pointer assuming it wouldn't be a problem.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by frostwolf View Post
    Thanks for the quick reply Jim.Compiling works fine for me using Bloodshed Dev C++, however I'll take your advice into account.Anyway I think I found the reason why my exe crashes.It appears to be a segmentation fault,which occurs because I was trying to use a null pointer assuming it wouldn't be a problem.
    Go into your DevC++ settings and find the spot that adjusts your warning levels... Set them to the highest available setting... -Wall, I believe.

    Now treat each warning and each error as an issue to be fixed.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Go into your DevC++ settings and find the spot that adjusts your warning levels... Set them to the highest available setting... -Wall, I believe.

    Now treat each warning and each error as an issue to be fixed.
    Thanks for the help CommonTater, I have now eliminated all warnings and errors that I get and the exe still crashes, but I hope this time people will be able to compile it without problems.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct nodedef{
                   struct nodedef *ptrL;
                   struct nodedef *ptrD;
                   struct nodedef *ptrU;
                   struct nodedef *ptrR;
                   int numb;
                   };
    
    typedef struct nodedef node;
    
    void null_node(node *);
    node *gridlist(void);
    node *triangle(void);
    node *half(void);
                   
    int main()
          {
          int choice=0,k;
          
          do{
             printf("\nMenu\n\n");
             printf("1. Option 1 : Grid\n");
             printf("2. Option 2 : Triangle\n");
             printf("3. Option 3 : Half grid\n");
             printf("9. Exit\n\n");    
          
          
          
          do{
             fflush(stdin);
             k=scanf("%d", &choice);
             if(k!=1){ printf("\nInvalid input.\n") ;
             }
             }while(k!=1);
             
             switch(choice){
                            case 1:printf("Placeholder 1");
                            gridlist();
                            break;
                            case 2:printf("Placeholder 2");
                            triangle();
                            break;
                            case 3:printf("Placeholder 3");
                            half();
                            break;
                            case 9:printf("Stopping program.\n");
                            return 0;
                            default: printf("Please choose again.");
    }
    
    }while(choice!=9);
    return 0;
    }
             
    node *gridlist(void){
    
         node *start, *currV, *prevV, *currH, *prevH, *next;
         int i,j,columns,rows;
         
         printf("Please specify how many rows and colums you'd like in the Grid option");
         scanf("%d%d",&rows,&columns);
        
         start=(node *)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         currH=start;
         prevH=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         
         //creating the first row first
         for(i=0;i<columns-1;i++){
                                currH->ptrR=(node *)malloc(sizeof(node));
                                prevH=currH;
                                currH=currH->ptrR;
                                null_node(currH);
                                currH->ptrL=prevH;
                                printf("Please type in your number\n");
                                scanf("%d", &currH->numb);
                                }
         //creating the rest of the structure one pointer that stays one row behind the current horizontal traversing through the rows;
         printf("\nTEST\n");
         for(i=0;i<rows-1;i++){
                               next=currV;
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV); 
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               currH=currV;
                               prevH=currV;
                               printf("\nTEST\n");
                               for(j=0;j<columns-1;j++){
                                                      currH->ptrR=(node *)malloc(sizeof(node));
                                                      prevH=currH;
                                                      currH=currH->ptrR;
                                                      null_node(currH);
                                                      currH->ptrL=prevH;
                                                      next=next->ptrR;
                                                      next->ptrD=currH;
                                                      currH->ptrU=next;  
                                                      printf("Please type in your number\n");
                                                      scanf("%d", &currH->numb);                       
    }
    }     
    
         
         printf("\nCurrH %p %p %p %d\n",currH,currH->ptrR,currH->ptrL,currH->numb); 
         printf("\nCurrV %p %p %d\n",currV,currV->ptrL,currV->numb);               
    return 0;             
    }
    
    void null_node(node *curr){
    
    
    
      curr->ptrL=NULL;
      curr->ptrU=NULL;
      curr->ptrD=NULL;
      curr->ptrR=NULL;
      
      return;
    }
    
    node *triangle(void){
         
         node *start, *currH, *prevH, *prevV, *currV, *next;
         int i,j,k,rowcount=1,rows;
         printf("Please specify how many rows you'd like in the Triangle option\n");
         scanf("%d",&rows);
         
         start=(node*)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         for(i=0;i<rows-1;i++){
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV);
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               next=prevV;
                               printf("\nTEST\n");
         //left side loop
                               for(j=0;j<rowcount;j++){
                                                       printf("\nTEST2\n");
                                                       currH=currV;
                                                       prevH=currV;
                                                       printf("\nTEST2\n");
                                                       currH->ptrL=(node *)(sizeof(node));
                                                       printf("\nTEST2\n");
                                                       prevH=currH;
                                                       currH=currH->ptrL;
                                                       printf("\nTEST3\n");
                                                       null_node(currH);
                                                       printf("\nTEST5\n");
                                                       currH->ptrR=prevH;
                                                       next=next->ptrL;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("\nTEST3\n");
                                                       printf("Please type in your number\n");
                                                       scanf("%d", &currH->numb);                                                   
                                                       }
     //resetting next to prevV for the right side loop
     //right side loop;                          
                                                 printf("TEST\n");
                               next=prevV;
                               for(k=0;k<rowcount;k++){
                                                       currH=currV;
                                                       prevH=currV;
                                                       currH->ptrR=(node *)(sizeof(node));
                                                       prevH=currH;
                                                       currH=currH->ptrR;
                                                       null_node(currH);
                                                       currH->ptrL=prevH;
                                                       next=next->ptrR;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("Please type in your number\n");            
                                                       scanf("%d", &currH->numb);
                                                       }
                                                       
                               rowcount++;
                                                      
         }
         return 0;
         }
    
    
    node *half(void){
         node *start, *currH, *prevH, *prevV, *currV, *next;
         int i,k,rowcount=1,rows;
         
         printf("Please specify how many rows you'd like in the Triangle option\n");
         scanf("%d",&rows);
         
         start=(node*)malloc(sizeof(node));
         null_node(start);
         currV=start;
         prevV=start;
         printf("Please type in your number\n");
         scanf("%d", &currV->numb);
         for(i=0;i<rows-1;i++){
                               currV->ptrD=(node *)malloc(sizeof(node));
                               prevV=currV;
                               currV=currV->ptrD;
                               null_node(currV);
                               currV->ptrU=prevV;
                               printf("Please type in your number\n");
                               scanf("%d", &currV->numb);
                               next=prevV;
                               printf("\nTEST\n");
    //right side loop                           
                                                for(k=0;k<rowcount;k++){
                                                       currH=currV;
                                                       prevH=currV;
                                                       currH->ptrR=(node *)(sizeof(node));
                                                       prevH=currH;
                                                       currH=currH->ptrR;
                                                       null_node(currH);
                                                       currH->ptrL=prevH;
                                                       next=next->ptrR;
                                                       next->ptrD=currH;
                                                       currH->ptrU=next;
                                                       printf("Please type in your number\n");            
                                                       scanf("%d", &currH->numb);
                                                       }                 
                               rowcount++;
                               
                               
    }
         
         
         
         return 0;
         }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I still get a few warnings:
    ||=== c_homework, Debug ===|
    main.c||In function ‘gridlist’:|
    main.c|120|warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘struct node *’|
    main.c|120|warning: format ‘%p’ expects type ‘void *’, but argument 3 has type ‘struct nodedef *’|
    main.c|120|warning: format ‘%p’ expects type ‘void *’, but argument 4 has type ‘struct nodedef *’|
    main.c|121|warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘struct node *’|
    main.c|121|warning: format ‘%p’ expects type ‘void *’, but argument 3 has type ‘struct nodedef *’|
    ||=== Build finished: 0 errors, 5 warnings ===|
    But I would say the biggest problem is that in some of your functions you are not actually returning what you should be returning. For example:
    Code:
    node *half(void) {
       .....
       return 0;
    }
    In this function you said you would be returning a node* but instead you return 0. Quite a few of your functions have this discrepancy.

    Jim

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    The warnings you get are for lines that shouldn't even be in that function, I was just using those printf statements to test, they will be removed.I really don't know what to put instead of return 0; or return. I was replicating this off my lecturer's example for creating the memory structure, which conluded the node grindlist(void) function with just return;. My main concern is that the code crashes at line 165, while calling a separate function to NULL the values of the pointer I'm using. This method worked in the previous node function and also in the outer loop.

  8. #8
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    Did you mean this (on line 160):
    Code:
    currH->ptrL=(node *)(sizeof(node));
    Or this:
    Code:
    currH->ptrL=(node *)malloc(sizeof(node));
    Just wondering...
    GNU GDB reports SIGSEGV on next use of currH->ptrL
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  9. #9
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    line 233: next=next->ptrR;
    next->ptrR is NULL. You can not assign NULL to a pointer (as in the next line)
    and expect to use the NULL pointer without crashing your program.
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Benji Wiebe View Post
    line 233: next=next->ptrR;
    next->ptrR is NULL. You can not assign NULL to a pointer (as in the next line)
    and expect to use the NULL pointer without crashing your program.
    Well, you CAN... but you're going to be very disappointed

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Yeah it all worked perfectly in my mind.I guess I'll just have to rethink the logic on this one.

    EDIT: I will probably paste the edited code tomorrow if I run into some more problems that I can't solve.Thanks for the help.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also you need to free your memory to avoid the constant memory leaks your program has at this time.

    Jim

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Also you need to free your memory to avoid the constant memory leaks your program has at this time.

    Jim
    Can you elaborate on that? I've started dealing with C just 3 months ago. I guess it should be some kind of fflush command, but which buffer?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by frostwolf View Post
    Can you elaborate on that? I've started dealing with C just 3 months ago. I guess it should be some kind of fflush command, but which buffer?
    It's very simple... if you created it with malloc(), calloc() or realloc()... you have to free() it when you're done with it.

    You took a tool out of your toolbox... you have to put it back when you're done.

    Memory allocation returns pointers into a program's heap memory... if you do not release these pointers when done, the program's heap keeps growing and growing until you run out of memory. In the most severe cases this has been known to crash systems.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    It's very simple... if you created it with malloc(), calloc() or realloc()... you have to free() it when you're done with it.

    You took a tool out of your toolbox... you have to put it back when you're done.

    Memory allocation returns pointers into a program's heap memory... if you do not release these pointers when done, the program's heap keeps growing and growing until you run out of memory. In the most severe cases this has been known to crash systems.
    Can you please give me an example using a piece of my code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Noob : Run time error I cannot figure out.
    By Cron in forum C Programming
    Replies: 18
    Last Post: 12-09-2011, 01:08 PM
  2. Im stuck and can't figure this out
    By jturner38 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2010, 04:51 PM
  3. Complete C noob...and completely stuck lol
    By JST212 in forum C Programming
    Replies: 6
    Last Post: 03-02-2008, 10:54 AM
  4. days
    By chrismiceli in forum C Programming
    Replies: 1
    Last Post: 08-09-2003, 06:31 PM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM