Pointer logic causing an access violation

1

One of my program methods to add a contact is giving the following error when executing:

Unhandled exception at 0x00111deb in G00290342.exe: 0xC0000005: Access violation reading location 0x00000050.

I have researched the error and found that it is caused by accessing memory already in use.I have narrowed it down to the pointer logic in my readFile method that is causing the 'curr' variable to be NULL.

The code it is breaking at is here:

while(curr->next != NULL)
    {
        curr = curr->next;

    }

This is the complete method for reference:

int addContact(struct contact *theList)
{
    struct contact *newContact, *curr;
    char fn[15],sn[15],ph[15],cmpy[15],eml[15];

    //create the new structure
    newContact = (struct contact *)malloc(sizeof(struct contact));
    if(newContact == NULL)
    {

        return(0);

    }
    //find the end of list
    curr = theList;
    //scroll through the list
    while(curr->next != NULL)
    {
        curr = curr->next;

    }
    //now have the last contact and the new one here
    printf("\nEnter a surname: ");
    gets(newContact->sname);

    printf("\nEnter a first name: ");
    gets(newContact->fname);

    printf("\nEnter a phone: ");
    gets(newContact->phone);

    printf("\nEnter a company: ");
    gets(newContact->company);

    printf("\nEnter an email: ");
    gets(newContact->email);

    //add the new contact to the end of the list

    curr->next = newContact;
    newContact->prev = curr;
    newContact->next = NULL;
    return(0);


}//end addContact

This is the main method where I'm reading the test file:

main()
{
    int sts,iChoice;
    struct contact *ptrList, *head;

    //head of sorted list
    struct contact *srtdList;

    srtdList = NULL;
    ptrList = NULL;

    ptrList = readFile("test.csv",ptrList);
    head = ptrList;

    /////menu for options
    system("cls");
    printf("\n\n\t\tWelcome to BV Contact Organizer\n\n");
    printf("\n\n\t\tEnter a number ranging from 1 to 6 for options.\n\n");
    printf("\n\t\t1. Search");
    printf("\n\t\t2. Add");
    printf("\n\t\t3. Sort");
    printf("\n\t\t4. Remove");
    printf("\n\t\t5. Edit");
    printf("\n\t\t6. Exit");
    printf("\n\n\t\tEnter your menu choice: ");
    fflush(stdin);
    scanf("%d", &iChoice);

    // user enters one of 6 values:
    // Search,add,sort,remove,exit or edit contacts


    switch(iChoice)
    {

        case 1:     // Add
        {
            sts = addContact(head);
            sts = writeListToFile("test.csv",head);
            while(ptrList != NULL)
            {
                printf("\n%s,%s,%s,%s,%s",ptrList->sname,ptrList->fname,ptrList->phone,ptrList->company,ptrList->email);
                ptrList = ptrList->next;

            }

            break;
        }
        case 2:     // Sort
        {
            //return the head of the sorted list to here
            srtdList = sortList(head,srtdList);
            head = srtdList;
            if(srtdList != NULL)
            {

                printf("\n\nSorted List");
                while(srtdList != NULL)
                {
                    printf("\n%s,%s,%s,%s,%s",srtdList->sname,srtdList->fname,srtdList->phone,srtdList->company,srtdList->email);
                    srtdList = srtdList->next;

                }

                sts = writeListToFile("testSort.csv",head);

            }
            else
            {
                printf("nothing to print");

            }
            printf("\n\n\n");
            system("pause");
            break;
        }
        case 3:     // Exit
        {
            printf("\n\nProgram exiting!...");
            break;
        }
        default:
        {
            printf("\n\nInvalid menu choice,please choose a number ranging from 1 to 6!...");
        }

    }//end of switch


    return(iChoice);

} // end of main

This is the struct contact definition as requested:

struct contact {
        char sname[15];
        char fname[15];
        char phone[15];
        char company[15];
        char email[15];
        struct contact *prev;
        struct contact *next;
};

This is the method used to readFile and is causing the problem:

struct contact *readFile(char * FName,struct contact *ptrList)
{

struct contact *head, *newContact;
FILE *fptr;
char oneLine[60];
char *sname, *fname, *phone,*company, *email;

head = ptrList;

fptr = fopen(FName,"r");

if(fptr == NULL)
{
    printf("\nCant open file!");
    return(ptrList);

}

fgets(oneLine, 55, fptr);
while(!feof(fptr))
{
    fgets(oneLine, 55, fptr);
    if(oneLine[strlen(oneLine)-1] == '\n')
    {
        oneLine[strlen(oneLine)-1] = '\0';

    }

    sname = strtok(oneLine,",");
    fname = strtok(NULL,",");
    phone = strtok(NULL,",");
    company = strtok(NULL,",");
    email = strtok(NULL,",");

    if(head == NULL)
    {
        head = (struct contact *)malloc(sizeof(struct contact));
        ptrList = head;
        strcpy(head->sname,sname);
        strcpy(head->fname,fname);
        strcpy(head->phone,phone);
        strcpy(head->company,company);
        strcpy(head->email, email);

        head->prev = NULL;
        head->next = NULL;


    }
    else
    {

        newContact = (struct contact *)malloc(sizeof(struct contact));
        head->next = newContact;
        newContact->prev = head;
        newContact->next = NULL;
        //copy the data to the new one
        strcpy(head->sname,sname);
        strcpy(head->fname,fname);
        strcpy(head->phone,phone);
        strcpy(head->company,company);
        strcpy(head->email,email);

        //move down the list so that the head variable
        //points to the last contact
        head = newContact;

    }

  }//end while

  fclose(fptr);
  return(ptrList);
}
c
pointers
memory-management
data-structures
asked on Stack Overflow Apr 23, 2013 by (unknown user) • edited Apr 23, 2013 by (unknown user)

3 Answers

2

So you are assigning curr to theList without any NULL checks:

curr = theList;

so when you get to the while if curr is indeed NULL then you will have access violation when you attempt to to do curr->next:

while(curr->next != NULL)
{
    curr = curr->next;
}
answered on Stack Overflow Apr 23, 2013 by Shafik Yaghmour
0

It says Access violation reading location 0x00000050 So you are trying to read a memory location which is actually some junk value. Trace theList value from the beginning, Also Wherever you are allocating memory, immediately do a memset(newContact,0,sizeof(struct contact)). After all these, if you still facing the problem, provide the code, there might be logic issue

answered on Stack Overflow Apr 23, 2013 by abasu • edited Apr 23, 2013 by abasu
0
curr = theList;    
 while(curr->next != NULL)
 {
    curr = curr->next;

}

the issue also might happen if last element in theList is not null and pointing to garbage memory location

answered on Stack Overflow Apr 23, 2013 by rahul maindargi

User contributions licensed under CC BY-SA 3.0