Thread: Access Violation(Segmentation Fault) error

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    11

    Access Violation(Segmentation Fault) error

    I'm using Dev C btw.
    We're basically trying to make a mail program? Logging in, sending mail to other registered users, etc.

    Each "e-mail" must be dynamically allocated(through the use of linked lists).

    The sendmail function takes:

    the message details(mail data) like recipients, the subject and the message
    the username of the account sending it
    and the address of the first node of the linked list of accounts.

    It encounters an access violation at the line where it says "while(temp->inbox->next != NULL)".

    The program is 1031 lines long so i simply posted the function. If you need any other data, please ask and I will post that section of the program

    Please help me resolve the problem

    Code:
    void sendmail(mail data, char username[], account *list)
    {
         int i=0;
         account *temp;
         inbox *message;
         message = malloc(sizeof(inbox));
         message->inbox = data;
         message->next = NULL;
         for(i=0;i<5;i++)
             message->inbox.recipient[i].chara[0] = '\0';
         strcpy(message->inbox.recipient[i].chara, username);
         temp = NULL;
         system("cls");
         for(i=0;i<5;i++)
         {
              if(strlen(data.recipient[i].chara) > 0)
              {     
                  printf("%s - Message sending... ", data.recipient[i].chara);
              
                  switch(usercheck(list, data.recipient[i].chara))
                  {
                        case -1: printf("Error: Username too short.\n\n");
                                 break;
                        case 2: printf("Error: Username too long.\n\n");
                                break;
                        case 3: printf("Error: Invalid username.\n\n");
                                break;
                        case 0: printf("Error: User does not exist.\n\n");
                                break;
                        case 1: temp = openmail(list, data.recipient[i].chara);
                                if(temp->inbox = NULL)
                                   temp->inbox = message;
                                else
                                {
                                    while(temp->inbox->next != NULL)
                                         temp->inbox = temp->inbox->next;
                                    temp->inbox->next = message;
                                    temp->inbox->next->next = NULL;
                                    printf("temp->inbox->next->inbox.recipient[i].chara = %s", temp->inbox->next->inbox.recipient[i].chara);
                                    printf("Message sent! Time sent: %s\n\n", ctime(&data.timenow));
                                    temp = NULL;
                                }
                                break;
                  }
              }
         }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nirvana619 View Post
    Code:
    case 1: temp = openmail(list, data.recipient[i].chara);
        if(temp->inbox = NULL)
            temp->inbox = message;
        else
        {
            while(temp->inbox->next != NULL)
            ...
    temp->inbox is set to NULL and then you attempt to dereference the NULL pointer, result: segfault.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    Oh. Lol, didn't see that. Sorry, so it should be temp->inbox == NULL right? Thanks

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by nirvana619 View Post
    Oh. Lol, didn't see that. Sorry, so it should be temp->inbox == NULL right? Thanks
    Yes, that's why certain other languages don't allow assignment in cases like this. To prevent the very unintended result you get.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    11
    Quote Originally Posted by Syscal View Post
    Yes, that's why certain other languages don't allow assignment in cases like this. To prevent the very unintended result you get.
    Thanks for replying within the day

    May I ask for help on another thing? Or should I make a diff thread? It's about file processing.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nirvana619 View Post
    Thanks for replying within the day

    May I ask for help on another thing? Or should I make a diff thread? It's about file processing.
    Separate threads for separate problems. If someone is searching the forums for a problem related to file processing, do you expect them to find it in a thread titled 'segmentation fault'?
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM