【发布时间】:2018-10-10 02:50:30
【问题描述】:
我正在尝试创建一个空堆栈,但不知何故,malloc 出现错误,即使使用调试器我也无法弄清楚。
我的调试器显示的消息是:
sysmalloc: Assertion (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0) failed.
Aborted
我应该如何解决它?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
int x;
int y;
int r;
int g;
int b;
}Pixel;
typedef int TypeKey;
typedef struct {
TypeKey Key;
Pixel P;
} TypeItem;
typedef struct Cell_str *Pointer;
typedef struct Cell_str {
TypeItem Item;
Pointer Next;
} Cell;
typedef struct {
Pointer Top, Bottom;
int Size;
} TypeStack;
void FEmpty(TypeStack *Stack)
{
Stack->Top = (Pointer)malloc(sizeof(Cell*));
Stack->Bottom = Stack->Top;
Stack->Top->Next = NULL;
Stack->Size = 0;
}
int Empty(const TypeStack *Stack){
return (Stack->Top == Stack->Bottom);
}
int size(TypeStack Stack)
{
return (Stack.Size) ;
}
int main(int argc, char *argv[])
{
Pixel P[500][500];;
TypeStack *Stack;
FEmpty(Stack);
return 0;
}
【问题讨论】:
-
我强烈建议您减少
typedefs,并且永远在 typedef 后面隐藏指针 - 这会导致代码难以理解。