Thread: segmentation fault (core dumped)?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    17

    segmentation fault (core dumped)?

    I am fairly new to using pointers and queues and such, but this is a program we're required to write for class and I finally got it to compile completely without any errors or warnings and now I go to run it with a.out and all I get is the message in the title of this post: "segmentation fault (core dumped)". does anyone know what this means or how to fix it? My code is below. I also included my Makefile because I don't have a lot of experience with those either and thought maybe that was the problem. Thanks in advance for any help!

    this is my queue.c


    Code:
     
      
    #include <stdio.h>
     #include <stdlib.h>
     #include "boolean.h"
     #include "queue.h" 
    
    int menu(void);
     
    int main(void){
      queue q;
     int selection, data;
     boolean quit=FALSE;
    
    init_queue(&q);
    while(!quit){
       scanf("%d", &selection);
       switch(selection){
         case 1:
           if(is_full()){
             printf("The queue is full\n");
           }
           else{
             printf("Please enter a number to add to the queue.\n");
             scanf("%d", &data);
             add(&q, data);
           }
         break;
         case 2:
           if (is_empty(q)){
              printf("The queue is already empty\n");
            }
            else{
              data = fetch(&q);
              printf("%d was removed\n\n", data);
          }
            break;
         case 3:
           print_queue(q);
           break;
         case 4:
           quit=TRUE;
         default:
           printf("Sorry, but that is not a valid selection.\n\n");
           break;
     }
     if(!quit) menu();
    }
     }
      
    int menu(void){
     printf("1. Add\n"); printf("2. Fetch\n"); printf("3. List\n"); printf("4. Quit\n"); printf("Enter a selection: "); 
    }
    This is my queue.h

    Code:
    #include <stdio.h>
    #include "boolean.h"
    #include <stdlib.h>  
    
    typedef struct queuenode{
        int data;     struct queuenode *next;
    } *node_pointer;
    
    typedef struct endpoint{
        node_pointer front;
        node_pointer back;
    }*queue;  
    
    void init_queue(queue *);
    boolean is_full(void);
    boolean is_empty(queue);
    void add(queue *, int);
    int fetch (queue *);
    void print_queue(queue);
    
     void init_queue(queue *q){   
    (*q) -> front = NULL;   
    (*q) -> back = NULL; 
    }
    
     boolean is_full (void){
     node_pointer temp;
     temp = (node_pointer) malloc(sizeof(struct queuenode));
     if (temp == NULL){
       return TRUE;  
    }
     else{
       free(temp);
       return FALSE;  
    }
    }
    
     boolean is_empty(queue q){
     if ((q -> front == NULL) && ((q -> back == NULL)))
      return TRUE;
     else   
    return FALSE;
    }  
    
    void add(queue *q, int add){
     queue queue;
     queue = *q;
     node_pointer temp;
     temp = (node_pointer) malloc(sizeof(struct queuenode));
     temp -> data = add;
     temp -> next = NULL;
     if (is_empty(queue)){
       (*q) -> front = temp;
       (*q) -> back = temp;
     }
     else{
       (*q) -> back -> next;
       (*q) -> back = temp;  
    }
    }
    
     int fetch (queue *q){
     node_pointer temp;
     int fetch_data;  
    temp = (*q) -> front;
     fetch_data = temp -> data;  
    if (((*q) -> front) == ((*q) -> back)){
       (*q) -> front = NULL;
       (*q) -> back = NULL;  
    }
     else{
       (*q) -> front -> next;
     free(temp);  return fetch_data;
     }
    }
    
    void print_queue (queue q){
     node_pointer temp;
     temp = q -> front;
    while (temp!=NULL){
    printf("%d\n", (temp -> data));
     temp = temp -> next;
     }
    and this is my make file

    Code:
    CC = gcc
    CFLAGS = -o a.out
    LDFLAGS = -lm
    
    OBJS = queue.o
    
    a.out: $(OBJS)
        $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS)
    
    clean: 
        rm $(OBJS) a.out
    
    print: enscript -Plab01 *.c *.h typescript
    Last edited by tkd_aj; 04-17-2012 at 08:31 PM.

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    is it just on my screen or are the first two parts of my code in a long line? they won't stay the way I wrote them for some reason...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, it is not just on your screen. How exactly did you edit your post? You had it in a text editor and copied and pasted your code, right? If so, which text editor, what line endings, and say, which operating system and web browser?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    I sent it to myself in an email through gmail as an attachment, opened it in notepad (which looked wrong like it does here) so i tried internet explorer to open it instead and it looked right there. then copied and pasted using the code tags.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    oh, and the text editor where it was all written was gedit. and im not sure what you mean by line endings.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh right. My suspicion is that you had gedit set to use *nix line endings on say, a Linux box, but then you opened this on Windows in a text editor that is only aware of Windows line endings (Notepad). Then you copied from Notepad, but because the line endings were not recognised as line endings, when you pasted into the text box input on this forum, you ended up with a result that was all on one line.

    My suggestion is for you to use a better text editor on Windows too

    For example, install Geany, Notepad++, or a host of other text editors more suited for dealing with source code, then use that text editor to open the file that you sent to yourself through Gmail, and paste it here for us to take a look at the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    all right sorry guys, I got everything fixed... I just went through and manually put all the spaces in.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Thanks, though it would be good if you could confirm or deny my theory about the many lines becoming a single line.

    Quote Originally Posted by tkd_aj
    all I get is the message in the title of this post: "segmentation fault (core dumped)". does anyone know what this means or how to fix it?
    Typically, this is a symptom of undefined behaviour. Undefined behaviour can be caused by say, accessing an array out of bounds, or more likely in this case, dereferencing a null pointer or an invalid pointer (e.g., to an object that no longer exists, or a pointer that never pointed to an object).

    My starting point would be to set breakpoints with your debugger and step through until you find the point where the segmentation fault occurs. The error most probably lies somewhere before that point (as in the flow of control).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's the queue code run through a pretty printer.
    Code:
    #include <stdio.h>
    #include "boolean.h"
    #include <stdlib.h>
    
    typedef struct queuenode{
        int data;
        struct queuenode *next;    
    } *node_pointer;
    
    typedef struct endpoint{
        node_pointer front;
        node_pointer back;
    } *queue;
    
    void init_queue(queue *);
    boolean is_full(void);
    boolean is_empty(queue);
    void add(queue *, int);
    int fetch (queue *);
    void print_queue(queue);
    
    void init_queue(queue *q){
        (*q)->front = NULL;
        (*q)->back = NULL;    
    }
    
    boolean is_full (void){
        node_pointer temp;
        temp = (node_pointer) malloc(sizeof(struct queuenode));
        if (temp == NULL){
            return TRUE;        
        }
        else{
            free(temp);
            return FALSE;   
        }    
    }
    
    boolean is_empty(queue q){
        if ((q->front == NULL) && ((q->back == NULL)))
            return TRUE;
        else
            return FALSE;    
    }
    
    void add(queue *q, int add){
        queue queue;
        queue = *q;
        node_pointer temp;
        temp = (node_pointer) malloc(sizeof(struct queuenode));
        temp->data = add;
        temp->next = NULL;
        if (is_empty(queue)){
            (*q)->front = temp;
            (*q)->back = temp;        
        }
        else{
            (*q)->back->next;
            (*q)->back = temp;   
        }    
    }
    
    int fetch (queue *q){
        node_pointer temp;
        int fetch_data;
        temp = (*q)->front;
        fetch_data = temp->data;
        if (((*q)->front) == ((*q)->back)){
            (*q)->front = NULL;
            (*q)->back = NULL;        
        }
        else{
            (*q)->front->next;
            free(temp);
            return fetch_data;   
        }
    }
    
    void print_queue (queue q){
        node_pointer temp;
        temp = q->front;
        while (temp!=NULL){
            printf("%d\n", (temp->data));
            temp = temp->next;   
        }
    }
    Your is_full function is madness. Get rid of it.

    Your is_empty function can just be
    Code:
    boolean is_empty(queue q) {
        return q->front == NULL;
    }
    These statements don't do anything.
    Code:
    // in add
    (*q) -> back -> next;
    // in fetch
    (*q) -> front -> next;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    You're not really speaking my language, lol.. This is only my second programming class and I'm not really sure what you mean by "set breakpoints with your debugger". I don't really have a debugger... All we use at school is linux command line style interface, with a text editor and gcc compiler.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tkd_aj
    I don't really have a debugger... All we use at school is linux command line style interface, with a text editor and gcc compiler.
    GDB is probably available. Unfortunately, gedit does not seem to have a GDB plugin. You use follow this tutorial to use GDB at the command line, or use a graphical front-end like DDD, if it is installed or if you can install it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Did you even notice that I showed you a couple of statements that DON'T DO ANYTHING. Could it be that THAT'S YOUR PROBLEM? BTW, compiling with proper warning levels would have pointed those lines out to you. gcc -Wall ....

    Also, look at this:
    Code:
    void add(queue *q, int add){
        queue queue;   // what? 
        queue = *q;    // but why?
        // those lines ultimately do nothing also
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by oogabooga View Post
    Did you even notice that I showed you a couple of statements that DON'T DO ANYTHING. Could it be that THAT'S YOUR PROBLEM? BTW, compiling with proper warning levels would have pointed those lines out to you. gcc -Wall ....

    Also, look at this:
    Code:
    void add(queue *q, int add){
        queue queue;   // what? 
        queue = *q;    // but why?
        // those lines ultimately do nothing also

    lol, I know that looks weird, but the reason i did it was because I thought that *q was a pointer and i kept getting a warning until i did that. I didn't understand why, but it made the warning go away... is it actually hurting me?

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    I removed the

    [code]

    queue queue;
    queue = *q;

    but it didn't do anything. I am still getting the segment error, but there is no longer a warning about that part, so that's good.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Did you fix these lines yet?
    Code:
    // in add
    (*q) -> back -> next;
    // in fetch
    (*q) -> front -> next;
    If you changed your gcc invocation in your makefile by adding -Wall to CFLAGS like this
    Code:
    CFLAGS = -Wall -o a.out
    then the compiler would point out those lines automatically. It will also notify you that something is wrong with your fetch routine's return. Nifty.

    (And why would you call your output a.out in a makefile? Still, that's your business.)

    Presumably in add you want
    Code:
    (*q) -> back -> next = temp;
    I can't comprehend what fetch is attemting to do.

    EDIT: I took another peek at fetch and I get it now. But you presumably want to free(temp) and return fetch_data whether or not it's the last node being removed.
    Last edited by oogabooga; 04-17-2012 at 11:00 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By jonagondo in forum C Programming
    Replies: 6
    Last Post: 01-04-2012, 03:56 PM
  2. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  3. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread