Can I free() static and automatic variables in C?

后端 未结 2 952
野趣味
野趣味 2020-12-11 16:31

The code is as follow :

#include 

int num = 3;   // Static external variable
int *ptr = #

int main(void)
{
 int num2 = 4;  // Autom         


        
2条回答
  •  醉梦人生
    2020-12-11 17:33

    Calling free() on a pointer not returned by memory allocating functions(malloc,calloc etc) causes Undefined Behavior.
    Your code has an Undefined Behavior, So the compiler does not need to give you any diagnostic of it and it can show any behavior, it might work, or crash, or literally do anything.

    Just avoid writing code which causes an Undefined Behavior is the only solution.

提交回复
热议问题