Search code examples
cmemory-managementfree

C free() function called on static memory (on not dynamically allocated memory)


What will happen if I call free() with non dynamically allocated pointer as the argument?

E.g

some_struct foo;
function(&foo);

void function(some_struct* param){
    free(param);
}

Thanks!


Solution

  • It's against the rules, so just about anything might happen. free() might print an error message. The program might crash with a segmentation violation or other memory access error. The error might be silently ignored. Or the error might lead to corruption of the malloc/free heap such that an error or crash might happen during some later call to malloc or free.