Thread: segfaulting!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    20

    segfaulting!

    Im getting a segmentation fault when I try to run the program and add new message. As soon as I input the 3 values it segfaults. Any help?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct MSG {
    	int priority;
    	int destination;
    	char *message;
    	int length;
    	struct MSG *nextPtr;
    };
    
    typedef struct MSG Message;
    
    int main ()
    {
    	struct MSG {
    		int priority;
    		int destination;
    		char *message;
    		int length;
    		struct MSG *nextPtr;
    	};
    
    	struct MSG std[100];
    	int I;
    	int n;
    	
    	
    	
    	int choice = 0;
    
    
    do
    {
    	printf("1.) Quit\n");
    	printf("2.) Transmit next message\n");
    	printf("3.) List messages by priority\n");
    	printf("4.) Summarize message list\n");
    	printf("5.) Add new message\n");
    	printf(":");
    	scanf("%d", &choice);
    	switch(choice)
    	{
    		case 1:
    			return 0;
    		case 2:
    			//transmit next message
    			break;
    		case 3:
    			//list messages by priority
    			break;
    		case 4:
    			//summarize message list
    			break;
    		case 5:
    			//add new message
    		        printf("How many messages would you like to enter?\n");
    			scanf("%d", &n);
    			printf("Enter a priority, destination, and message:");
    			for (I=0; I < n; I++)
    			{
    				scanf("%d%d%s", &std[I].priority, &std[I].destination, &std[I].message);
    			}
    			printf("\ntest info:");
    			for (I=0; I < n; I++)
    			{
    				printf("%d%d%s\n", std[I].priority, std[I].destination, std[I].message);
    			}
    			break;
    		default:
    			printf("Invalid entry, please try again.\n");
    			break;
    	}
    }while (choice <= 5);
    
    	return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    your message pointer needs to be initialized before stuff can be stored in it
    Last edited by ಠ_ಠ; 07-08-2009 at 04:14 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your program is essentially doing this:
    Code:
    char *s;
    scanf("%s", &s);
    There are two problems there. First: you're trying to write to a location that has no memory allocated. s points nowhere (or rather, its value is indeterminate, but that's not important). You have to allocate space. You can either make s an array (which allocates the chars for you), or use malloc() to set aside some memory.

    Second, you're passing &s, which has type "pointer to pointer to char", but %s expects "pointer to char". Though you've been conditioned to use & with scanf(), it's not for use with strings. Instead:
    Code:
    /* Either of these allocates space */
    char *s = malloc(1024); /* should make sure s is not NULL now */
    char a[1024];
    /* Both of these are now correct */
    scanf("%s", s);
    scanf("%s", a);
    scanf() isn't safe being used like this, but I presume you'd rather learn about that at a later date. scanf() is actually a pain to use properly for most applications.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Thanks for the help guys, I have this code now which freezes after I input the values:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main ()
    {
    	struct MSG {
    		int priority;
    		int destination;
    		char *message;
    		int length;
    		struct MSG *nextPtr;
    	};
    
    	struct MSG std[100];
            char message[100];
    	int I;
    	int n;
    	
    	
    	
    	int choice = 0;
    
    
    do
    {
    	printf("1.) Quit\n");
    	printf("2.) Transmit next message\n");
    	printf("3.) List messages by priority\n");
    	printf("4.) Summarize message list\n");
    	printf("5.) Add new message\n");
    	printf(":");
    	scanf("%d", &choice);
    	switch(choice)
    	{
    		case 1:
    			return 0;
    		case 2:
    			//transmit next message
    			break;
    		case 3:
    			//list messages by priority
    			break;
    		case 4:
    			//summarize message list
    			break;
    		case 5:
    			//add new message
    		        printf("How many messages would you like to enter?\n");
    			scanf("%d", &n);
    			printf("Enter a priority, destination, and message:");
    			for (I=0; I < n; I++)
    			{
    				scanf("%d%d%s", &std[I].priority, &std[I].destination, &std[I].message);
    			}
    			printf("\ntest info:");
    			for (I=0; I < n; I++)
    			{
    				printf("%d%d%s\n", std[I].priority, std[I].destination, std[I].message);
    			}
    			break;
    		default:
    			printf("Invalid entry, please try again.\n");
    			break;
    	}
    }while (choice <= 5);
    
    	return 0;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CMakesMeSad :( View Post
    Thanks for the help guys, I have this code now which freezes after I input the values:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main ()
    {
    	struct MSG {
    		int priority;
    		int destination;
    		char *message;
    		int length;
    		struct MSG *nextPtr;
    	};
    
    	struct MSG std[100];
            char message[100];
    	int I;
    	int n;
    	
    	
    	
    	int choice = 0;
    
    
    do
    {
    	printf("1.) Quit\n");
    	printf("2.) Transmit next message\n");
    	printf("3.) List messages by priority\n");
    	printf("4.) Summarize message list\n");
    	printf("5.) Add new message\n");
    	printf(":");
    	scanf("%d", &choice);
    	switch(choice)
    	{
    		case 1:
    			return 0;
    		case 2:
    			//transmit next message
    			break;
    		case 3:
    			//list messages by priority
    			break;
    		case 4:
    			//summarize message list
    			break;
    		case 5:
    			//add new message
    		        printf("How many messages would you like to enter?\n");
    			scanf("%d", &n);
    			printf("Enter a priority, destination, and message:");
    			for (I=0; I < n; I++)
    			{
    				scanf("%d%d%s", &std[I].priority, &std[I].destination, &std[I].message);
    			}
    			printf("\ntest info:");
    			for (I=0; I < n; I++)
    			{
    				printf("%d%d%s\n", std[I].priority, std[I].destination, std[I].message);
    			}
    			break;
    		default:
    			printf("Invalid entry, please try again.\n");
    			break;
    	}
    }while (choice <= 5);
    
    	return 0;
    }
    I and i are not the same thing, although when I quote the code it magically has fixed itself.

    What do you do to cause the error, and how far do you get?

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    I and i are not the same thing, although when I quote the code it magically has fixed itself.

    What do you do to cause the error, and how far do you get?
    I noticed that, in my code they're both "I" but when I post it hear they convert to "i"s for some reason. I get to the point where I add a new message, tell it how many messages I want to add, then as soon as I hit enter after inputing the first message it freezes.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CMakesMeSad :( View Post
    I noticed that, in my code they're both "I" but when I post it hear they convert to "i"s for some reason. I get to the point where I add a new message, tell it how many messages I want to add, then as soon as I hit enter after inputing the first message it freezes.
    That's not frozen. You said you wanted to input <n> messages; you've got to type <n> messages.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by cas View Post
    scanf() isn't safe being used like this, but I presume you'd rather learn about that at a later date. scanf() is actually a pain to use properly for most applications.
    Im here to learn so if you have better alternatives id love to hear them

  9. #9
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by CMakesMeSad :( View Post
    Im here to learn so if you have better alternatives id love to hear them
    fgets
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by ಠ_ಠ View Post
    fgets
    Thanks, Ill definitely look into it!

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I still get a segmentation fault as soon as I enter a number

    You should post the current version of the code, otherwise we'd just be making wild guesses.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I would recommend starting with a fixed-size structure and no dynamic allocation first, until you have got everything else worked out.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Hey everyone, Ive been working on this all day and have made some good progress, however Im stuck again. Im working on the option of listing the messages by priority. However, no matter what priority I enter, it will only list the first message i enter.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    
    struct MSG {
    	int priority;
    	int destination;
    	char *message;
    	int length;
    	struct MSG *nextPtr;
    }*node, *t;
    
    typedef struct MSG message;
    
    
    void add(struct MSG **n, int priority1, int destination1, char *message1);
    
    int main ()
    {
    	struct MSG *temp;
    	int choice;
    	int num1, num2;
    	char text[MAX];
     		
    do
    {
    	printf("1.) Quit\n");
    	printf("2.) Transmit next message\n");
    	printf("3.) List messages by priority\n");
    	printf("4.) Summarize message list\n");
    	printf("5.) Add new message\n");
    	printf(":");
    	scanf("%d", &choice);
    	switch(choice){
    
    		case 1:
    			return 0;
    		case 2:
    			//transmit next message
    			if (node==NULL)
    			{
    				printf("No messages left!\n");
    				break;
    			}
    			
    			else
    			{
    				temp=node;
    				while(temp != NULL)
    				{
    					printf("%d", temp -> priority);
    					printf("%dn", temp -> destination);
    					char *loc=(char *) malloc (strlen(temp -> message));
    					strcpy(loc, temp -> message);
    					printf("%s", loc);
    					free(loc);
    					temp=temp -> nextPtr;
    				}
    				printf("test=%d %d %s\n", t-> priority, t-> destination, t-> message);
    			}
    
    
    			break;
    		case 3:
    			//list messages by priority
    				printf("Enter priority of the messages you would like to retrieve:\n");
    				scanf("%d", &pnum);
    				
    				temp=node;
    				while(temp != NULL)
    				{
    					//printf("%d", temp -> priority);
    					//printf("%dn", temp -> destination);
    					char *loc=(char *) malloc (strlen(temp -> message));
    					strcpy(loc, temp -> message);
    					//printf("%s", loc);
    					free(loc);
    					temp=temp -> nextPtr;
    					if (t -> priority == pnum)
    					{
    						printf("%d %d %s\n", t -> priority, t -> destination, t -> message);
    					}
    				}
    
    
    			break;
    		case 4:
    			//summarize message list
    			break;
    		case 5:
    			//add new message
    			printf("Enter priority, destination and message:");
    			scanf("%d%d", &num1, &num2);
    			fgets(text, MAX, stdin);
    			add(&node, num1, num2, text);	
    			break;
    		default:
    			printf("Invalid entry, please try again.\n");
    			break;
    	}
    }while (choice <= 5);
    
    	return 0;
    }
    
    
    
    
    
    void add(struct MSG **n, int priority1, int destination1, char *message1)
    {
    	struct MSG *temp, *node1;
    	
    	if(*n==NULL)
    	{
    		temp=(struct MSG *) malloc (sizeof(struct MSG));
    		temp -> priority = priority1;
    		temp -> destination=destination1;
    		temp -> message=(char *) malloc (strlen(message1)+1);
    		strcpy(temp -> message, message1);
    		temp -> nextPtr=NULL;
    		*n=temp;
    		t=temp;
    		printf("test=%d %d %s\n", t->priority, t->destination, t->message);
    	}
    
    	else
    	{
    		temp=*n;
    		while(temp->nextPtr != NULL)
    			temp=temp -> nextPtr;
    		node1 = (struct MSG *) malloc(sizeof(struct MSG));
    		node1 -> priority=priority1;
    		node1 -> destination=destination1;
    		node1 -> message=(char*) malloc(strlen(message1)+1);
    		strcpy(node1 -> message, message1);
    		node1 -> nextPtr = NULL;
    		temp -> nextPtr = node1;
    	}
    
    }
    Last edited by CMakesMeSad :(; 07-08-2009 at 11:06 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't printing anything other than the exact priority match. Is that what you want?

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by quzah View Post
    You aren't printing anything other than the exact priority match. Is that what you want?

    Quzah.
    Thats kind of what I want it do. Lets say input the following 4 messages:
    1 5 message1
    5 10 message2
    1 6 message3
    2 7 message4

    The first number being the priority, I want to be able to list all the messages that fall under a priority of my choice. So if I pick priority 1, I want the program to ouput

    1 5 message1
    1 6 message3

    This is what its doing right now:
    *******************
    input priority: 5 or 2
    nothing happens

    input priority 1
    1 5 message1
    1 5 message1
    1 5 message1
    1 5 message1
    *******************
    I hope that makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is strncpy segfaulting here?
    By Calef13 in forum C Programming
    Replies: 3
    Last Post: 12-29-2008, 03:27 PM
  2. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 08:48 PM
  3. Why is it segfaulting?
    By XSquared in forum C Programming
    Replies: 7
    Last Post: 03-30-2004, 06:52 AM
  4. a segfaulting algorythm
    By demonus in forum C Programming
    Replies: 8
    Last Post: 08-11-2003, 08:06 AM