Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "CRT detected that the application wrote to memory after end of heap buffer" mean?

I am having trouble with this code. It breaks at the free(q->izv) function and i get a debug error saying:

CRT detected that the application wrote to memory after end of heap buffer

I have no idea what that means so i would be grateful for any help I get.

    typedef struct izvodjaci{
        char *izv;
        int broj;
        struct izvodjaci *sled;
    }IZV;

    obrisi_i(IZV *p){
        while (p){
            IZV *q;
            q = p;
            p = p->sled;
            if (!strcmp(q->izv,"UNKNOWN")) free(q->izv);
            free(q);
        }
    }

Thanks in advance

like image 457
user3699827 Avatar asked Jun 04 '14 13:06

user3699827


1 Answers

What does “CRT detected that the application wrote to memory after end of heap buffer” mean?

Suppose you allocate a heap buffer:

char* buffer = malloc(5);

OK, buffer now points to five chars on the heap.

Suppose you write six chars into that buffer:

buffer[0] = 'a';
buffer[1] = 'b';
buffer[2] = 'c';
buffer[3] = 'd';
buffer[4] = 'e';
buffer[5] = '\0';

You have now corrupted the heap; you were only allowed to write five chars and you wrote six.

The program is now permitted to do anything whatsoever. It can work normally, it can crash, it can send all your passwords to hackers in China, anything.

Your implementation apparently chooses the best possible choice, which is "inform you that you made a mistake". You should be very, very happy that this is what happened, instead of any of the horrible alternatives. Unfortunately it informs you when the buffer is freed, and not when you made the mistake, but be happy that you got an error at all.

like image 148
Eric Lippert Avatar answered Sep 28 '22 01:09

Eric Lippert