【问题标题】:Thread1: EXC_BAD_ACCESS (code =1, address = 0x0) in C线程 1:C 中的 EXC_BAD_ACCESS(代码 =1,地址 = 0x0)
【发布时间】:2015-03-11 16:35:15
【问题描述】:

我面对

线程1:EXC_BAD_ACCESS(代码=1,地址=0x0)

每当我尝试从输入扫描字符串到 char * 变量时。 我不知道它为什么会发生,因为一切似乎都是正确的。

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

标题

struct date {
    int year;
    int month;
    int day;
};

日期结构

struct patientData {
    unsigned int code;
    char name[11];
    char family[21];
    unsigned int age;
    char MF;
    char disease[11];
    unsigned int numOfVisits;
    struct date *date1;
    struct date *date2;
    struct date *date3;
    struct patientData *nextPtr;

};

患者数据结构

int isEmpty (struct patientData *sPtr);
void visit ( struct patientData **returned , unsigned int code);
void Insert (struct patientData **sPtr, unsigned int code , char *name , char *family , unsigned int age ,char gender, int tmgh);
void insertDisease (struct patientData **sPtr   , char *name , char *family , char *disease );
struct patientData *searchCode (struct patientData **sPtr  , unsigned int code, int *returnval);
struct patientData *searchName (struct patientData **sPtr , char *name  , char *family);
void searchDate (struct patientData **sPtr , int year , int month , int day );
void delete (struct patientData **sPtr  );
void report (struct patientData **sPtr  );

函数; 这里(主要)是问题发生的地方。

int main() {

    char *choice;

    unsigned int code;
    char name[11];
    char family[21];;
    char disease[11];
    int searchCodeReturnValue;
    unsigned int age;
    char gender;
    int tmgh;
    int year , month , day;

    struct patientData *startPtr = NULL;


    puts("Enter one of the following options:");
    puts("Visit");
    puts("InsertDisease");
    puts("search");
    puts("Delete");
    puts("END");

    scanf("%s",choice);
    while (strcmp(choice, "END") != 0) {
        if (strcmp(choice, "Visit") == 0) {
            printf("Enter the code:\n");
            scanf("%5ui",&code);
            struct patientData *a = searchCode(&startPtr,code,&searchCodeReturnValue);
            if (searchCodeReturnValue == 1){
                visit(&a , code);
            }
            else if (searchCodeReturnValue == 0){
                printf("Enter name:\n");
                scanf("%10s",name);
                printf("Enter family:\n");
                scanf("%20s",family);
                printf("Enter age:\n");
                scanf("%ui",&age);
                printf("Enter gender:\n");
                scanf("%c",&gender);
                printf("Enter num of last visits:\n");
                scanf("%i",&tmgh);
                Insert(&startPtr , code , name , family , age , gender , tmgh);

            }
        }
        else if ( strcmp(choice, "InsertDisease")== 0) {
            printf("Enter name:\n");
            scanf("%10s",name);
            printf("Enter family:\n");
            scanf("%20s",family);
            printf("Enter disease:\n");
            scanf("%10s",disease);
            struct patientData *namesearch = searchName(&startPtr, name, family);
            insertDisease ( &namesearch , name , family , disease );
        }
        else if (strcmp(choice, "Search")== 0) {
            puts("Choose the way you wanna search: \n 1- by code \n 2- by first and last name \n 3- by Date");
            int choiceNum;
            scanf("%i",&choiceNum);
            if (choiceNum == 1) {
                printf("Enter the code:\n");
                scanf("%5ui",&code);
                searchCode(&startPtr, code , &searchCodeReturnValue);
            }
            else if ( choiceNum == 2){
                printf("Enter name:\n");
                scanf("%10s",name);
                printf("Enter family:\n");
                scanf("%20s",family);
                searchName(&startPtr ,name , family );
            }
            else if ( choiceNum == 3){
                printf("Enter year:\n");
                scanf("%i",&year);
                printf("Enter month:\n");
                scanf("%i",&month);
                printf("Enter day:\n");
                scanf("%i",&day);
                searchDate(&startPtr , year , month , day);
            }
            else
                puts("Wrong entry");
        }
        else if (strcmp(choice, "delete")== 0) {
            delete(&startPtr);
        }
        else if (strcmp(choice, "Report") == 0) {
            report(&startPtr);
        }
        else if (strcmp(choice, "END") == 0)
            return 0;

        else{
            puts("wrong!");
            return 0;
        }
    }
    return 0;
}

【问题讨论】:

    标签: c error-handling runtime-error


    【解决方案1】:
    char *choice;
    

    内存没有分配给指针,你正在做

    scanf("%s",choice);
    

    为指针分配内存,稍后尝试扫描值给它。

    choice = malloc(30); /* Size can be anything of your wish */
    

    所以你正在访问未初始化的指针,这将导致未定义的行为。

    使用完此内存后,您需要释放它

    free(choice);
    

    【讨论】:

    • 真的吗?但是我以前用过这种风格,我从来没有遇到过这样的错误,为什么它需要内存分配? '#include int main() { char *mine; scanf("%s",我的); printf("%s\n",我的);返回0; }'
    • @user21087 是的,为了将您的值存储到某个内存位置,您需要为其分配内存。你可以使用malloc() 来做同样的事情,如图所示
    【解决方案2】:

    您正在取消引用无效指针,choice 被声明为 char 指针并且从未初始化,您不需要它是 char 指针,可以将 choice 声明为 char 数组同样,它包含的最长字符串似乎是 "InsertDisease",它有 13 个字符,所以这样声明 choice

    char choice[14];
    

    并将scanf 更改为

    scanf("%13s", choice);
    

    这样可以防止缓冲区溢出和内存泄漏(如果你没有正确使用freechoice,这将由malloc 引起)。 p>

    我看到您也没有重新扫描 choice 值,这将使您的循环无限,您应该将其添加到循环的顶部,并将其删除循环外,然后将循环编写为

    while (1) {
        scanf("%13s", choice);
        .
        .
       /* check the content of choice with strcmp and process the requested command */
        .
        .
    }
    

    在循环中你有一个if (strcmp(choice, "END") == 0) return 0;,所以它应该负责结束循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      相关资源
      最近更新 更多