Thread: I am getting "segment fault" for simple program. Can help any one

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    I am getting "segment fault" for simple program. Can help any one

    HI ,

    I am trying to execute below simple program. But i am getting "Segmentation fault" after selecting choice 1.

    Please hlep me regarding this.

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    struct node
    {
    int data;
    struct node *addr;
    };
    
    struct node *first;
    
    int main()
    {
    int choice;
    printf("%d \n",first);
    while(1)
    {
    printf("enter the choice\n");
    printf (" 1.display\n 3.quit\n");
    scanf("%d",&choice);
    switch (choice)
    {
    	case 1:
    		display();
    		break;
    	case 3:
    		quit();
    }
    }
    return 0;
    }
    
    int display()
    {
    struct node *temp;
    temp=(struct node *)malloc(1 * sizeof(struct node));
    printf("entered into display function\n");
    if (temp == NULL)
    {
    printf("Memory is not allocated to create new node\n");
    return 0;
    }
    	
    printf ("%d\n",temp);
    printf ("%d\n",temp);
    printf ("%d\n",first->data);
    if ( first->addr == NULL)
    {
    printf ("List is empty\n");
    return 0;
    }
    else
    {
    temp=first;
    printf ("The elements in Linked list are\n");
    while (temp->addr !=NULL)
    {
    printf ("%d\n",temp->data);
    temp = temp->addr;
    }
    }
    
    return 0;
    }
    
    int quit()
    {
    printf("entered into exit\n");
    exit (0);
    }
    o/p:

    enter the choice
    1.display
    3.quit
    1
    entered into display function
    145903624
    145903624
    Segmentation fault








    Regards
    Koti

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    70+ posts, and you still can't post indented code?
    SourceForge.net: Indentation - cpwiki
    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.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI ,

    Any problem in my code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. It lacks proper indentation to make it readable without modification.
    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

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI ,

    I am able to see the code in my borwser .



    Please find the attachment.

    if any problem in my process, please let me know





    I am getting &quot;segment fault&quot; for simple program. Can help any one-code_format-jpg

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by nkrao123@gmail. View Post
    I am able to see the code in my borwser .
    You are completely missing the point being made here.
    Proper indentation makes the code easier to read and to follow the code flow.

    Right way to post code
    Code:
    int main()
    {
        while(true)
        {
            if(1)
            {
                printf("Hello world!\n");
            }
            break;
        return 0;
    }
    Wrong way:
    Code:
    int main()
    {
    while(true)
    {
    if(1)
    {
    printf("Hello world!\n");
    }
    break;
    return 0;
    }
    See how easy it is to spot the missing closing bracket in the first example compared to the second one?
    If you want us to help you then don't make us have to work by editing your code just to be able to understand it.

  7. #7
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi ,

    i corrected the code. Please find the below code

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    struct node
    {
    	int data;
    	struct node *addr;
    };
    
    struct node *first;
    
    int main()
    {
    	int choice;
    	printf("%d \n",first);
    	while(1)
    	{
    		printf("enter the choice\n");
    		printf (" 1.display\n 3.quit\n");
    		scanf("%d",&choice);
    		switch (choice)
    		{
    			case 1:
    				display();
    				break;
    			case 3:
    				quit();
    		}
    	}
    return 0;
    }
    
    int display()
    {
    	struct node *temp;
    	temp=(struct node *)malloc(1 * sizeof(struct node));
    	printf("entered into display function\n");
    	if (temp == NULL)
    	{
    		printf("Memory is not allocated to create new node\n");
    	return 0;
    	}
    	
    	printf ("%d\n",temp);
    	printf ("%d\n",temp);
    	printf ("%d\n",first->data);
    	if ( first->addr == NULL)
    	{
    		printf ("List is empty\n");
    	return 0;
    	}
    	else
    	{
    		temp=first;
    		printf ("The elements in Linked list are\n");
    		while (temp->addr !=NULL)
    		{
    			printf ("%d\n",temp->data);
    			temp = temp->addr;
    		}
    	}	
    
    return 0;
    }
    
    int quit()
    {
    	printf("entered into exit\n");
    	exit (0);
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Better, but there are a few lines which could be improved, notably some return statements. FYI, do not mix spaces and tabs in your editor - it may look good there, but on a forum, it's still a mess.


    printf ("%d\n",first->data);
    if ( first->addr == NULL)
    Perhaps check something is NULL before trying to dereference it?

    Specifically, check first == NULL before anything else.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Stray Character" when running a simple encryption program.
    By manasij7479 in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2011, 09:20 AM
  2. "Segment Violation" error, fopen("r+")
    By jiboso in forum C Programming
    Replies: 1
    Last Post: 03-10-2011, 09:57 AM
  3. [Segmentation Fault] fopen("filename","w")
    By gibbofresco in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 04:32 AM
  4. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM
  5. "Segment fault?"
    By kinghajj in forum C Programming
    Replies: 5
    Last Post: 09-28-2003, 10:41 AM