segmentation fault(core dump)

This is a discussion on segmentation fault(core dump) within the Linux Programming forums, part of the Platform Specific Boards category; i was trying to run a c program where i was trying to implement the stack data structure using linked ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    26

    segmentation fault(core dump)

    i was trying to run a c program where i was trying to implement the stack data structure using linked list. my linux version is ubuntu 10.04 and while running i get the error segmentation fault(core dump) after which the program doesnot run any more on the terminal. i am giving the coding as well.]
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct stack
    {
    int data;
    struct stack *link;
    int top;
    };
    
    typedef struct stack stack;
    
    void push(struct stack *,int item);
    int pop(struct stack *);
    void display(struct stack *);
    
    int main()
    {
    struct stack s;
    int noofelements_push,elements_push;
    int noofelements_pop;
    int i,j;
    
    printf("\n Enter no. of elements to push:");
    scanf("%d",&noofelements_push);
    for(i=0;i<noofelements_push;i++)
    {
    printf("\n Enter the elements:");
    scanf("%d",&elements_push);
    push(&s,elements_push);
    fflush(stdin);
    }
    
    display(&s);
    
    printf("\n Enter no. of elements to pop:");
    scanf("%d",&noofelements_pop);
    for(i=0;i<noofelements_pop;i++)
    {
    j=pop(&s);
    printf("\n the poped element is:%d",j);
    }
    
    display(&s);
    
    return (0);
    }
    
    void push(struct stack *s,int item)
    {
    stack *newnode,*temp;
    newnode=(stack *)malloc(sizeof(stack));
    newnode->data=item;
    newnode->link=NULL;
    if(s==NULL)
    {
    s=newnode;
    }
    else
    {
    temp=s;
    while(temp->link!=NULL)
    {
    temp=temp->link;
    }
    temp->link=newnode;
    }
    }
    
    int pop(struct stack *s)
    {
    stack *temp,*prev;
    int i;
    temp=s;
    while(temp->link!=NULL)
    {
    prev=temp;
    temp=temp->link;
    }
    i=temp->data;
    free(temp);
    prev->link=NULL;
    return(i);
    }
    
    void display(struct stack *s)
    {
    while(s!=NULL)
    {
    printf("\n %d",s->data);
    s=s->link;
    }
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Your #1 problem is you need to adopt a conventional coding style -- one with indents. Something like this:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct stack
    {
            int data;
            struct stack *link;
            int top;
    };
    
    typedef struct stack stack;
    
    void push(struct stack *,int item);
    int pop(struct stack *);
    void display(struct stack *);
    
    int main()
    {
            struct stack s;
            int noofelements_push,elements_push;
            int noofelements_pop;
            int i,j;
    
            printf("\n Enter no. of elements to push:");
            scanf("%d",&noofelements_push);
            for(i=0;i<noofelements_push;i++)
            {
                    printf("\n Enter the elements:");
                    scanf("%d",&elements_push);
                    push(&s,elements_push);
                    fflush(stdin);// BIG NO NO here!!
            }
    
            display(&s);
    
            printf("\n Enter no. of elements to pop:");
            scanf("%d",&noofelements_pop);
            for(i=0;i<noofelements_pop;i++)
            {
                    j=pop(&s);
                    printf("\n the poped element is:%d",j);
            }
    
            display(&s);
    
            return (0);
    }
    
    void push(struct stack *s,int item)
    {
            stack *newnode,*temp;
            newnode=(stack *)malloc(sizeof(stack));
            newnode->data=item;
            newnode->link=NULL;
            if(s==NULL)
            {
                    s=newnode;
            }
            else
            {
                    temp=s;
                    while(temp->link!=NULL)
                    {
                            temp=temp->link;
                    }
                    temp->link=newnode;
            }
    }
    
    int pop(struct stack *s)
    {
            stack *temp,*prev;
            int i;
            temp=s;
            while(temp->link!=NULL)
            {
                    prev=temp;
                    temp=temp->link;
            }
            i=temp->data;
            free(temp);
            prev->link=NULL;
            return(i);
    }
    
    void display(struct stack *s)
    {
            while(s!=NULL)
            {
                    printf("\n %d",s->data);
                    s=s->link;
            }
    }
    There is a rather rash thing you've done. Take that out and try again. If you want to flush stdin, you READ IT. Something like while (getchar() != '\n'); will work for you.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Maybe it's the lack of indentation?
    Indent style - Wikipedia, the free encyclopedia

    Well it isn't, but the lack of indentation is a big turn-off for anyone wanting to look at the code.

    Is it as badly formatted in your source code editor?
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    prev is uninitialized in pop, and you're dereferencing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 09:59 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  3. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  4. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 PM
  5. segmentation core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 03:43 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