简体   繁体   中英

Error while trying to allocate memory using malloc for custom struct

I am attempting to allocate some dynamic memory for a custom stack in my c program. However, I receive the error 0xC0000005: Access violation writing location 0x00000014 during the malloc call.

Here is my struct definition and my function calling 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);
    }
}

Thanks for any help!

I see two problems with the posted code (which the compiler should have emitted warnings for):

  • Implicit declaration of Init() and InitSpace() which means they will have a return type of int
  • Init() does not return a value.

The problem is this line in InitCrossword() function:

currentSpace->nextSpace = nextSpace;

on first iteration of the for loop currentSpace is NULL.

I'm pretty sure the syntax you're using creates your struct with type space_tag and Space is actually a variable (see http://www.cplusplus.com/doc/tutorial/structures/ ).

Try this code:

#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));
}

Edited based on example code posted

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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