【问题标题】:Error while trying to allocate memory using malloc for custom struct尝试使用 malloc 为自定义结构分配内存时出错
【发布时间】:2012-09-27 21:53:23
【问题描述】:

我正在尝试为我的 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);
    }
}

感谢您的帮助!

【问题讨论】:

  • 能发个小程序重现这个吗?
  • 看来你的堆已经被之前的东西破坏了。此外,您似乎在 Windows 上运行(通过错误代码);我不完全确定,但我认为 Windows 可能对线程使用的堆栈有一些特定的要求/期望。我不确定您是否可以将线程的堆栈设置为任意内存块。
  • 我发布了一个示例程序。是的,我正在运行 Windows。
  • @atob 我认为您正在尝试向lastSpace 写入一些内容,然后再为其分配有效内存。 lastSpace 与结构开头的偏移量为 20(或 0x00000014)。此外,发布甚至无法编译的代码也无济于事。
  • 嗨 atob,欢迎来到 Stack Overflow。顺便说一句,您可以使用 clang 静态分析器立即找到您的错误。 (它实际上在你的代码中发现了三个错误,其中只有一个是 hmjd 给出的答案。)Check it out yourself 找到另外两个。

标签: c memory-management malloc


【解决方案1】:

我发现发布的代码有两个问题(编译器应该发出警告):

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

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

currentSpace->nextSpace = nextSpace;

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

【讨论】:

  • 我刚刚发布了存根代码。我的实际程序中有其余信息。
  • 你解决了,非常感谢。我已经为此工作了很长时间!
【解决方案2】:

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

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

【讨论】:

  • 你的代码产生了错误,所以我在space_tag前面加了struct:return (struct space_tag *) malloc(sizeof(struct space_tag));它仍然产生同样的错误
【解决方案3】:

Init(); 中缺少返回函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 2018-06-06
    • 2015-06-27
    • 2020-06-05
    • 2015-01-05
    • 1970-01-01
    • 2021-05-29
    • 2012-06-12
    相关资源
    最近更新 更多