尝试使用malloc为自定义结构分配内存时出错
我试图在我的c程序中为自定义堆栈分配一些动态内存。但是,我在malloc调用期间收到错误0xc000005:访问冲突写入位置0x00000014 以下是我的结构定义和调用malloc的函数:尝试使用malloc为自定义结构分配内存时出错,c,memory-management,malloc,C,Memory Management,Malloc,我试图在我的c程序中为自定义堆栈分配一些动态内存。但是,我在malloc调用期间收到错误0xc000005:访问冲突写入位置0x00000014 以下是我的结构定义和调用malloc的函数: #include <stdio.h> #include <stdlib.h> #define EMPTY -1; typedef enum boolean_tag { TRUE, FALSE } Boolean; typedef enum direction_tag { ACRO
#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);
}
}
#包括
#包括
#定义空-1;
typedef枚举布尔值_标记{TRUE,FALSE}布尔值;
typedef枚举方向\标记{交叉,向下}应答方向/*填字游戏中答案的方向*/
typedef结构应答标签{
/*单个“答案”是组成链接列表的单个节点*/
国际应答号码;
回答方向;
char*answerString;/*答案的值(以chars为单位)*/
struct answer_tag*nextAnswer;/*指向链接列表中的下一个答案*/
}答复;
typedef结构空间标签{
/*板内的单个空间*/
int numberOfCurrentAnswers;/*当前有多少个答案穿过该空间*/
字符值;
int x;
int-y;
结构空间\标签*在空间后面;
结构空间_标记*nextSpace;
}空间;
无效答案(答案*);
空格*InitCrossword();
空格*InitSpace();
无效程序关闭清理(空格*);
main(){
空间*板;
board=InitCrossword();
程序关闭清洁(板);
}
无效初始答案(答案*答案键){
}
空格*InitCrossword(){
int xLimit,yLimit;/*限制集*/
int xTraverse,yTraverse;/*用于遍历的坐标变量*/
Space*currentSpace=NULL;
空间*nextSpace;
printf(“请输入电路板的尺寸:x y\n”);
scanf(“%d%d”,&xLimit,&yLimit);
对于(xTraverse=0;xTraversex=xTraverse;
nextSpace->y=y行程;
nextSpace->numberOfCurrentAnswers=0;
nextSpace->value=空;
nextSpace->behindSpace=currentSpace;
currentSpace->nextSpace=nextSpace;
currentSpace=nextSpace;
}
}
while(currentSpace->behindSpace!=NULL)
currentSpace=currentSpace->behindSpace;
返回电流空间;
}
Space*InitSpace(){
返回(空格*)malloc(sizeof(空格));
}
无效程序关闭清理(空间*当前空间){
空间*nextSpace;
while(currentSpace!=NULL){
nextSpace=currentSpace->nextSpace;
自由(当前空间);
}
}
谢谢你的帮助 我很确定您使用的语法创建了类型为
space\u tag
的结构,而space
实际上是一个变量(请参阅)
请尝试以下代码:
#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));
}
#包括
#包括
typedef结构空间{
int numberOfCurrentAnswers;
字符值;
int x;
int-y;
结构空间*lastSpace;
结构空间*nextSpace;
};
main(){
结构空间*空间;
space=Init();
}
空格*Init(){
结构空间*nextSpace;
nextSpace=InitSpace();
}
Space*InitSpace(){
返回(空格*)malloc(sizeof(空格));
}
根据发布的示例代码进行编辑您缺少
Init()中的返回函数代码>我发现发布的代码有两个问题(编译器应该对此发出警告):
Init()
和InitSpace()
的隐式声明,这意味着它们的返回类型为int
Init()
不返回值
问题在于InitCrossword()函数中的这一行:
currentSpace->nextSpace = nextSpace;
在for
循环currentSpace
的第一次迭代中,它是空的。您能发布一个小程序来复制它吗?看来您的堆已经被以前的东西损坏了。此外,您似乎正在Windows上运行(根据错误代码);我我不太确定,但我认为Windows对线程使用的堆栈可能有一些特殊的要求/期望。我不确定是否可以将线程堆栈设置为任意内存块。我发布了一个示例程序。是的,我正在运行windows。@atob我想您在为lastSpace
分配有效内存之前,正在尝试向其写入内容lastSpace
与结构开头的偏移量为20(或0x00000014)。此外,发布甚至没有编译的代码也没有任何帮助。顺便说一句,您可以使用clang静态分析器立即找到您的bug。(它实际上在您的代码中发现了三个bug,其中只有一个是hmjd给出的答案。)为了找到另外两个bug,您的代码生成了一个错误,所以我在space_标记前面添加了struct:return(struct space_标记*)malloc(sizeof(struct space_标记));它仍然生成了与我刚才发布的存根代码相同的错误。我在我的实际程序中有剩余的信息。你解决了它,非常感谢。我已经做了很长时间了!