Why do problems of this kind appear?

    1 answer 1

    char* f() { char s[100]; // страшное вычисление s return s; } 

    Submitted? call char * s = f(); . Fine? seems Yes.

    But when we start using this pointer, that char s[100]; from a function it can be overwritten long ago by something else (this memory is allocated in the stack only for the duration of the function ). And we get a dangling pointer — i.e. he is and even indicates somewhere ... Only there can be anything.

    Or -

     s = malloc(...); .... free(s); 

    The value of s remains - free does not change it, but it indicates the memory that is freed, can be allocated again, overwritten ... or, for example, deleted again.

    So clearer?

    • You can also mention the "double free" - vp_arth
    • @vp_arth Well, this is still a consequence of "hanging", not a reason :) But you, of course, are right. Now I will add. - Harry