尝试显示列表时,读取访问冲突(0xCCCCCCD0)

时间:2016-03-25 12:26:52

标签: c exception linked-list

我存储到列表然后尝试显示它,但是当我调用显示功能时,我收到此错误。我不确定问题是什么,因为它没有给我任何警告或错误。

typedef struct {

    // Variables - Details on person taking survey
    int ppsNo;
    char firstName[15];
    char lastName[15];
    char address[30];
    char email[30];

    // Groups - Statistics are broken down based on these groups
    int age;
    int income;
    int gender;

    // Survey - What the survey questions will consist of
    int exercise;
    int alcohol;
    int cigarettes;

}survey_t; // Structure defined

struct list_el{

    survey_t data;
    struct list_el* next;
}; // List for passing values into the struct

我接受值

void addSurvey(struct list_el** pointer) {

    struct list_el* data;
    data = (struct list_el*)malloc(sizeof(struct list_el));

    printf("\n===== Adding Someone to the Survey =====\n");

    // Depending on the number added, they output will be different
    // Taking in details and passing through the list
    printf("Enter PPS Number: ");
    scanf("%d", &data->data.ppsNo);
    printf("Enter First Name: ");
    scanf("%s", data->data.firstName);
    printf("Enter Last Name: ");
    scanf("%s", data->data.lastName);
    printf("Enter Address: ");
    scanf("%s", data->data.address);
    printf("Enter Email Address: ");
    scanf("%s", data->data.email);

    // Number added will determine the age group
    printf("\nEnter age group\n");
    printf("\t1: 18 - 20\n");
    printf("\t2: 20 - 30\n");
    printf("\t3: 30 - 50\n");
    printf("\t4: 50 - 65\n");
    printf("\t5: 65+\n");
    scanf("%d", &data->data.age);

    if (data->data.age > 5 && data->data.age < 1) {
        printf("Error - Re Enter Age group\n");
        scanf("%d", &data->data.age);
    }

    // Number added will determine income group
    printf("\nIncome Bracket - What category do you fall under\n");
    printf("\t1: No Income\n");
    printf("\t2: Less than 20,000\n");
    printf("\t3: Less than 40,000\n");
    printf("\t4: Less than 60,000\n");
    printf("\t5: Less than 80,000\n");
    printf("\t6: Less than 100,000\n");
    printf("\t7: Greater than 100,000\n");
    scanf("%d", &data->data.income);

    if (data->data.income > 7 && data->data.income < 1) {
        printf("Error - Re Enter Income\n");
        scanf("%d", &data->data.income);
    }

    // Number added will determine gender
    printf("\nGender\n");
    printf("\t1: Male\n");
    printf("\t2: Female\n");
    scanf("%d", &data->data.gender);

    if (data->data.gender > 2 && data->data.gender < 1) {
        printf("Error - Re Enter Gender\n");
        scanf("%d", &data->data.gender);
    }

    // Number will be added to determine how often they exercise
    printf("\nExercise - How often do you exercise\n");
    printf("\t1: Never\n");
    printf("\t2: Less than three times per week\n");
    printf("\t3: Less than five times per week\n");
    printf("\t4: More than five times per week\n");
    scanf("%d", &data->data.exercise);

    if (data->data.exercise > 4 && data->data.exercise < 1) {
        printf("Error - Re Enter Exercise\n");
        scanf("%d", &data->data.exercise);
    }

    // Number will be added to determine how many units they drink
    printf("\nAlcohol - How many units of Alcohol do you drink per week\n");
    printf("\t1: None\n");
    printf("\t2: Less than 2 Units per week\n");
    printf("\t3: Less than 3 Units per week\n");
    printf("\t4: More than 4 Units per week\n");
    scanf("%d", &data->data.alcohol);

    if (data->data.alcohol > 4 && data->data.alcohol < 1) {
        printf("Error - Re Enter Alcohol\n");
        scanf("%d", &data->data.alcohol);
    }


    // Number will be added to determine how many cigarattes per week they smoke
    printf("\nCigarettes - How many Cigarettes do you smoke per week\n");
    printf("\t1: None\n");
    printf("\t2: Less than 20 cigarettes per week\n");
    printf("\t3: Less than 40 cigarettes per week\n");
    printf("\t4: More than 40 cigarettes per week\n");
    scanf("%d", &data->data.alcohol);

    if (data->data.alcohol > 4 && data->data.alcohol < 1) {
        printf("Error - Re Enter Alcohol\n");
        scanf("%d", &data->data.alcohol);
    }

}

要显示的功能

void displayAll(struct list_el *pointer) {

    struct list_el *temp;
    temp = pointer;

    if (pointer == NULL) {
        printf("Nothing to Display");
    }

    else {
        while (temp != NULL) {
            printf("\nDisplaying All Surveys\n");
            printf("Name: %s %s", temp->data.firstName, temp->data.lastName);
            printf("\nPPS: %d", temp->data.ppsNo);
            printf("\nAddress: %s", temp->data.address);
            printf("\nAddress: %s", temp->data.email);

            // Switch statement that determines the age group depending on the value
            switch (temp->data.age) {
            case 1:
                printf("\n18 - 20");
                break;

            case 2:
                printf("\n20 - 30");
                break;

            case 3:
                printf("\n30 - 50");
                break;

            case 4:
                printf("\n50 - 65");
                break;

            case 5:
                printf("\n65+");
                break;
            }

            switch (temp->data.income) {

            case 1:
                printf("\nNo Income");
                break;

            case 2:
                printf("\nLess than 20,000");
                break;

            case 3:
                printf("\nLess than 40,000");
                break;

            case 4:
                printf("\nLess than 60,000");
                break;

            case 5:
                printf("\nLess than 80,000");
                break;

            case 6:
                printf("\nLess than 100,000");
                break;

            case 7:
                printf("\nGreater than 100,000");
                break;
            }

            switch (temp->data.age) {

            case 1:
                printf("\nMale");
                break;

            case 2:
                printf("\nFemale");
                break;
            }

            switch (temp->data.exercise) {

            case 1:
                printf("\t1: Never\n");
                break;

            case 2:
                printf("\nExercises less than three times per week");
                break;

            case 3:
                printf("\nExercises less than five times per week");
                break;

            case 4:
                printf("\n Exercises more than five times per week");
                break;
            }

            switch (temp->data.alcohol) {

            case 1:
                printf("\nNo Units\n");
                break;

            case 2:
                printf("\nLess than 2 Units per week");
                break;

            case 3:
                printf("\nLess than 3 Units per week");
                break;

            case 4:
                printf("\nMore than 4 Units per week");
                break;
            }

            switch (temp->data.cigarettes) {

            case 1:
                printf("\nNo Cigarettes");
                break;

            case 2:
                printf("\nLess than 20 cigarettes per week");
                break;

            case 3:
                printf("\nLess than 40 cigarettes per week");
                break;

            case 4:
                printf("\nMore than 40 cigarettes per week");
                break;
            }

            temp = temp->next;
        }
    }

}

如果有人能够解释错误的含义,我将非常感谢,以备将来使用,所以我知道将来如何处理它。

2 个答案:

答案 0 :(得分:1)

未初始化的指针。在调试模式下,堆栈通常填充0xCC值,以便更快地检测错误。

答案 1 :(得分:0)

错误表示您正在尝试访问(读/写)非法地址的内存。通常,这意味着您取消引用非法指针值。

您的列表似乎不完整。

在此代码中:

void addSurvey(struct list_el** pointer) {

    struct list_el* data;
    data = (struct list_el*)malloc(sizeof(struct list_el));
    ...

似乎你永远不会使用pointer。换句话说,您似乎没有将新元素添加到列表中。你错过了什么:

*pointer = data;

此外,您应该初始化新元素的next,即

data->next = NULL;