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,
This is not at all correct:
char str[6]
.When you call malloc() or any other allocation function, memory will be allocated on the heap. This is the only memory that can be freed. When you declare a static string, as you've done in your example, the string is allocated at compile time in another memory segment. The same goes for the str
pointer itself which is allocated on the stack, and thus cannot be freed either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With