What happens when you try to free() already freed memory in c?

后端 未结 14 1037
天命终不由人
天命终不由人 2020-12-09 08:19

For example:

char * myString = malloc(sizeof(char)*STRING_BUFFER_SIZE);
free(myString);
free(myString);

Are there any adverse side effects

14条回答
  •  自闭症患者
    2020-12-09 08:23

    The admittedly strange macro below is a useful drop-in replacement for wiping out a few classes of security vulnerabilities as well as aid debugging since accesses to free()'d regions are more likely to segfault instead of silently corrupting memory.

    #define my_free(x) do { free(x); x = NULL; } while (0)
    

    The do-while loop is to help surrounding code more easily digest the multiple-statements. e.g. if (done) my_free(x);

提交回复
热议问题