繁体   English   中英

尝试使用malloc为自定义结构分配内存时出错

[英]Error while trying to allocate memory using malloc for custom struct

我试图在我的c程序中为自定义堆栈分配一些动态内存。 但是,我在malloc调用期间收到错误0xC0000005:访问冲突写入位置0x00000014。

这是我的struct定义和调用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);
    }
}

谢谢你的帮助!

我看到发布的代码存在两个问题(编译器应该发出警告):

  • Init()InitSpace()隐式声明意味着它们的返回类型为int
  • Init()不返回值。

问题是InitCrossword()函数中的这一行:

currentSpace->nextSpace = nextSpace;

for循环的第一次迭代中for currentSpace为NULL。

我非常确定您使用的语法会创建类型为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));
}

根据发布的示例代码进行编辑

您缺少Init();的返回函数Init();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM