-2

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;
};

2 Answers2

0

There is absolutely nothing wrong with the code you pasted, other than the fact it's way to short for us to be certain exactly what's causing the issue.

However, the assertion you're seeing is almost certainly due to a corrupt heap. It's the sort of thing that's caused by allocating x bytes and then writing, for example, x + 42 bytes to that area. That tends to corrupt the memory control information in your heap.

So I would suggest first looking at any memory allocations in your code to see if there's an issue with them.

Or, here's a thought, post more code so we can actually help you out in a more substantial way :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Finally I found what the problem was. I had something like char *thing = malloc(sizeof thing) but it has to be like char *thing = malloc(sizeof *thing)