1

我添加联系人的程序方法之一是在执行时出现以下错误:

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

我研究了该错误,发现它是由访问已在使用的内存引起的。我已将其缩小到导致“curr”变量为 NULL 的 readFile 方法中的指针逻辑。

它正在破坏的代码在这里:

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

    }

这是完整的方法供参考:

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

这是我读取测试文件的主要方法:

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

这是所要求的结构联系定义:

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

这是用于 readFile 的方法并导致问题:

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);
}
4

3 回答 3

2

所以你分配currtheList没有任何NULL检查:

curr = theList;

因此,当您到达whileifcurr确实NULL时,您将在尝试执行以下操作时遇到访问冲突curr->next

while(curr->next != NULL)
{
    curr = curr->next;
}
于 2013-04-23T18:35:43.673 回答
0

它说Access violation reading location 0x00000050所以你正在尝试读取一个实际上是一些垃圾值的内存位置。从头开始跟踪 theList 值,此外,无论您在哪里分配内存,立即执行memset(newContact,0,sizeof(struct contact)). 毕竟,如果您仍然遇到问题,请提供代码,可能存在逻辑问题

于 2013-04-23T18:35:21.900 回答
0
curr = theList;    
 while(curr->next != NULL)
 {
    curr = curr->next;

}

如果最后一个元素theListnot null并且指向垃圾内存位置,也可能会发生此问题

于 2013-04-23T18:42:34.470 回答