Using pointer after free()

后端 未结 5 1845
抹茶落季
抹茶落季 2020-11-27 22:54

During my tests, I have found that it is possible to use pointer after free(). I have the following code:

typedef struct{

    int module_id;
    int adc_id;         


        
5条回答
  •  心在旅途
    2020-11-27 23:15

    Why might a chicken continue to run and jump around despite it's head being chopped off? Note that this doesn't always happen; some chickens might cease moving immediately, while others might continue to run around for months. When it does happen, it happens because it happens. Does that mean we should cut our pets heads off?

    Like cutting our pets heads off, using memory that has been freed is a bad idea. That doesn't mean it will produce negative results; your program might continue to run as expected on your machine, while if (fubar) (where fubar is your invalid pointer) might be enough to cause your program to malfunction on other implementations.

    This idea freedom for the implementation to define (or not) the behaviour is formally known as undefined behaviour; the behaviour is undefined because the C standard, a set of documents dictating how C implementations should behave, doesn't define the behaviour. Therefore, like cutting our pets heads off, we should avoid all undefined behaviour.

提交回复
热议问题