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

The code is as follows:

#include <stdlib.h>

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

int main(void)
{
 int num2 = 4;  // Automatic variable
 int *ptr2 = &num2;

 free(ptr);  //Free static variable
 free(ptr2); //Free automatic variable

 return 0; 
}

I am trying to compile the above code and it works, I am curious if a function can free()release both a static variable and an automatic variable? Or basically does nothing?

+5
source share
2 answers

free() , (malloc, calloc ..) Undefined .
Undefined Behavior, - , , - .

, Undefined - .

+12

. free , malloc .

num . num2 main , main .

, free. , malloc/free . free , - , . , malloc, - . , - .

+5

All Articles