I'm making a C program where I have to use -ansi and -pedantic. I want to read the stdin input but when I call getchar() the program crashes. Here is the line that makes the error :
while((data = getchar()) != EOF) {
When I run it normally it says
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)
And with 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
EDIT:
Here is my code with allocations
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]);
}
And the header :
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;
};