cmalloccalloc

free() function without malloc or calloc


quick question

Can you use the free() function without having to prior call a malloc ??

ei.

void someFunc( void )
{
   char str[6] = {"Hello"};

   //some processing here ....

   free(str);
}

I get no compiling errors but Does this work or is it correct at all ?

Thank you,


Solution

  • This is not at all correct:

    1. You cannot free a static array such as char str[6].
    2. free() should only be called on memory you allocated (or on NULL).