我正在制作一个必须使用-ansi 和-pedantic 的C 程序。我想读取标准输入,但是当我调用 getchar() 时程序崩溃了。这是导致错误的行:
while((data = getchar()) != EOF) {
当我正常运行时它说
Calculator: malloc.c:2394: 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 (core dumped)
和 GDB
Program received signal SIGABRT, Aborted.
0x00007ffff7a42428 in __GI_raise (sig=sig@entry=6)
at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory
编辑:
这是我的分配代码
struct Expression* initParse(int argc, char const *argv[]) {
struct Expression *expr = malloc(sizeof expr);
enum WaitFor next = MinusNext | NumberNext | NewBraquetNext;
testStdin(argc, argv);
if(!expr)
exit(1);
expr->level = 0;
expr->part = 0;
expr->parts = malloc(sizeof expr->parts);
if(!expr->parts)
exit(1);
expr->parts[0] = malloc(sizeof expr->parts[0]);
if(!expr->parts[0])
exit(1);
expr->parts[0][0] = malloc(sizeof expr->parts[0][0]);
if(!expr->parts[0][0])
exit(1);
expr->parts[0][0]->type = MainPartType;
expr->parts[0][0]->priority = 0;
return parse(expr, next, argv[1]);
}
和标题:
enum WaitFor
{
MinusNext = 1 << 0,
OperatorNext = 1 << 1,
NumberNext = 1 << 2,
NewBraquetNext = 1 << 3,
EndBraquetNext = 1 << 4,
FinalEndNext = 1 << 5
};
struct Part
{
void *content;
int priority;
char *parent;
enum PartType type;
};
struct Expression
{
struct Part ***parts;
int level;
int part;
struct Part lastNumber;
};