Heap Damage in C

int main ()
{
    int * b;
    b = (int*) malloc (1);
    *b=110000;
    free (b);
    return 0;
}

Why does a bunch happen when free (b);?

IMO, heap damage already occurs at *b=110000;.

+3
source share
4 answers

malloc()An argument is the number of bytes to place. You need to use:

b = (int*) malloc(sizeof(int));

You allocated a block too small, and then wrote more bytes than you allocated, which overwrites the accounting information next to the block, corrupting the heap.

+12
source

* b = 110000; , int , . b = (int *) malloc (sizeof (int)), int * b char * b, malloced char *. , , 128 (- char) * b.

EDIT: - , - . .

+5

*b=11000, free(b), , .

( ) , . , malloc free ( ).

+4

, , , .

char *, int *, -128 127 * b .

0

Source: https://habr.com/ru/post/1769328/


All Articles