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

后端 未结 14 1036
天命终不由人
天命终不由人 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:42

    Here's the chapter and verse.

    If the argument [to the free function] does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined. (ISO 9899:1999 - Programming languages — C, Section 7.20.3.2)

    0 讨论(0)
  • 2020-12-09 08:45

    Not so clever. Google for double free vulnerabilities. Set your pointer to NULL after freeing to avoid such bugs.

    0 讨论(0)
提交回复
热议问题