C程序中的exc_bad_access

时间:2010-08-01 03:29:38

标签: c exc-bad-access

好的,我知道这里有另外一个关于“exc_bad_access”的问题,但这似乎与Objective-C和iPhone开发有关,而我的只是常规C.我是C的新手,我几乎完成了我的第一次程序,直到出现此错误。我一直试图弄明白这一天,我疯了。任何帮助表示赞赏。 越野车功能:

void edit (int i){
    char* z;
    char* y;
    char compare1[] = "on bobbin\b\b\b\b";
    char compare2[] = "not on bobbin";
    char compare3[] = "have\b\b\b\b\b\b";
    char compare4[] = "don't have";
    char wrapedit[] = "wrapped";
    char haveedit[] = "have";
    char editing[9];

    FILE *wrappedlist = fopen("../../wrapped", "r+");
    FILE *havelist = fopen("../../havelist", "r+");

    fseek(wrappedlist, i*14, SEEK_SET);
    fseek(havelist, i*11, SEEK_SET);

    printf("Edit? (y=yes, n=no)");
    fgets(z, 2, stdin);

    if ((*z=='y') && (strncmp(haveslist[i], compare4, (size_t)1) == 0)) {
        printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
        fgets(y, 2, stdin);

        if (*y=='y') {
            fputs(compare3, havelist);
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        else if(*y=='n'){
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        printf("Invalid input.");
        return;
    }

    else if ((*z=='y') && (strncmp(haveslist[i], compare3, (size_t)1) == 0)) {
        fpurge(stdout);
        printf("Edit \"wrapped\" or \"have\"?");
        fpurge(stdin);
        fgets(editing, 9, stdin);

        len = strlen(editing);
        editing[len-1]='\0';

        if (strcmp(editing, wrapedit)==0) {

        if (strncmp(wrapped[i], compare1, (size_t)1)==0) {

            printf("Switch \"on bobbin\" to \"not on bobbin\"? (y=yes, n=no)");
            fgets(y, 2, stdin);
            if (*y=='y') {

                fputs(compare2, wrappedlist);
                fclose(wrappedlist);
                fclose(havelist);
                return;
            }
            else if(*y=='n'){
                fclose(wrappedlist);
                fclose(havelist);
                return;
            }
        }
            else if(strncmp(wrapped[i], compare2, (size_t)1)==0){

                fpurge(stdout);
                printf("Switch \"not on bobbin\" to \"on bobbin\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {

                    fwrite(compare1, (size_t)strlen(compare1), 1, wrappedlist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
        }
                fpurge(stdout);
                printf("Invalid input.");
        }
            fpurge(stdout);
            printf("You don't want to edit wrapped apparently.");
            fclose(wrappedlist);
            fclose(havelist);
            return;
        }
        else if(strcmp(editing, haveedit)==0){

            if (strncmp(haveslist[i], compare3, 1) == 0){

                printf("Switch \"have\" to \"don't have\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {
                    fputs(compare4, havelist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                printf("Invalid input.");
            }
            else if(strncmp(haveslist[i], compare4, 1)==0){

                printf("Switch \"don't have\" to \"have\"? (y=yes, n=no)");
                fgets(y, 2, stdin);
                if (*y=='y') {
                    fputs(compare3, havelist);
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;
                }
                else if(*y=='n'){
                    fclose(wrappedlist);
                    fclose(havelist);
                    return;             
            }
        }
            printf("Invalid input.");
        }
            printf("Not editing.");

        fclose(wrappedlist);
        fclose(havelist);

        return;
    }
    else if(*z=='n'){
        fclose(wrappedlist);
        fclose(havelist);
        return;
    }

    printf("Invalid entry");
    fclose(havelist);
    fclose(wrappedlist);
    return;
}

我可以在“编辑”后输入一个字符到fgets吗?提示,但后来我得到了exc_bad_access错误。请帮忙,谢谢.code:

2 个答案:

答案 0 :(得分:7)

这是因为你的指针指向没有记忆。

char* z; // pointer points nowhere
/* snip */
fgets(z, 2, stdin); // writing to pointer that points nowhere: boom!

这会尝试将z中的两个下一个字符放入stdin。但是,z指出无处可用。你需要它指向现有的内存:单独声明一个指针不足以获得它旁边的内存。

你可能想要一个2个字符的缓冲区:

char z[2];

使用此代码,z将成为指向2个字符的足够内存的指针。 (在C中,可以在期望指针的任何地方传递数组。)

我的代码看起来并不太远,但y会遇到同样的问题。

答案 1 :(得分:2)

您尚未为zy 分配任何内存,它们未被初始化,因此fgets正在写入随机地址。