0

C プログラムのカスタム スタックに動的メモリを割り当てようとしています。ただし、malloc 呼び出し中にエラー 0xC0000005: アクセス違反の書き込み場所 0x00000014 が表示されます。

これが私の構造体定義と私の関数呼び出しmallocです:

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

#define EMPTY -1;

typedef enum boolean_tag { TRUE, FALSE } Boolean;
typedef enum direction_tag { ACROSS, DOWN } AnswerDirection;    /*The direction of an answer in the crossword*/

typedef struct answer_tag {
    /*A single 'Answer' is a single node making up a linked list*/
    int answerNumber;
    AnswerDirection direction;
    char *answerString;         /*The answer's value in chars*/
    struct answer_tag *nextAnswer;      /*Points to the next answer in the linked list*/
} Answer;

typedef struct space_tag {
    /*A single space inside of a board*/
    int numberOfCurrentAnswers; /*How many Answers currently cross through the space*/
    char value;
    int x;
    int y;
    struct space_tag *behindSpace;
    struct space_tag *nextSpace;
} Space;

    void InitAnswers(Answer *);
    Space *InitCrossword();
    Space *InitSpace();
    void ProgramClosingCleaning(Space *);

main(){
Space *board;
board = InitCrossword();
ProgramClosingCleaning(board);
}

void InitAnswers(Answer *answerKey){

}

Space *InitCrossword(){
int xLimit, yLimit;         /*Limits set*/
int xTraverse, yTraverse;   /*Coordinate variables to use in traversing*/
Space *currentSpace = NULL;
Space *nextSpace;           
printf("Please enter the size of the board: x y\n");
scanf("%d %d", &xLimit, &yLimit);
for (xTraverse = 0; xTraverse < xLimit; xTraverse++){
    for (yTraverse = 0; yTraverse < yLimit; yTraverse++){
        nextSpace = InitSpace();
        nextSpace->x = xTraverse;
        nextSpace->y = yTraverse;
        nextSpace->numberOfCurrentAnswers = 0;
        nextSpace->value = EMPTY;
        nextSpace->behindSpace = currentSpace;
        currentSpace->nextSpace = nextSpace;
        currentSpace = nextSpace;
    }
}
while (currentSpace->behindSpace != NULL)
    currentSpace = currentSpace->behindSpace;
return currentSpace;
}

Space *InitSpace(){
return (Space *) malloc(sizeof(Space));
}

void ProgramClosingCleaning(Space *currentSpace){
Space *nextSpace;
while (currentSpace != NULL){
    nextSpace = currentSpace->nextSpace;
    free(currentSpace);
    }
}

助けてくれてありがとう!

4

3 に答える 3

2

投稿されたコードには 2 つの問題があります (コンパイラが警告を発する必要があります)。

  • Init()andの暗黙的な宣言はInitSpace()、戻り値の型がint
  • Init()値を返しません。

問題は、InitCrossword()関数内の次の行です。

currentSpace->nextSpace = nextSpace;

forループの最初の反復でcurrentSpaceは NULL です。

于 2012-09-27T22:02:00.720 に答える
0

あなたが使用している構文は、型space_tagを持つ構造体を作成し、Space実際には変数であると確信しています(http://www.cplusplus.com/doc/tutorial/structures/を参照)。

このコードを試してください:

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

typedef struct Space {
    int numberOfCurrentAnswers;
    char value;
    int x;
    int y;
    struct Space *lastSpace;
    struct Space *nextSpace;
};

main(){
    struct Space *space;
    space = Init();
}

Space *Init(){
    struct Space *nextSpace;
    nextSpace = InitSpace();
}

Space *InitSpace(){
    return (Space *) malloc(sizeof(Space));
}

投稿されたサンプルコードに基づいて編集

于 2012-09-27T21:59:47.553 に答える
0

return 関数がありませんInit();

于 2012-09-27T22:01:56.007 に答える