free() function without malloc or calloc

jramirez picture jramirez · Nov 5, 2010 · Viewed 9.9k times · Source

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,

Answer

Zabba picture Zabba · Nov 5, 2010

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).