Unable to free const pointers in C
Solution 1:
Several people have posted the right answer, but they keep deleting it for some reason. You need to cast it to a non-const pointer; free
takes a void*
, not a const void*
:
free((char*)str);
Solution 2:
Your code is reversed.
This:
char* name="Arnold";
const char* str=(const char*)malloc(strlen(name)+1);
Should look like this:
const char* name="Arnold";
char* str=(char*)malloc(strlen(name)+1);
The const
storage type tells the compiler that you do not intend to modify a block of memory once allocated (dynamically, or statically). Freeing memory is modifying it. Note, you don't need to cast the return value of malloc(), but that's just an aside.
There is little use in dynamically allocating memory (which you are doing, based on the length of name
) and telling the compiler you have no intention of using it. Note, using meaning writing something to it and then (optionally) freeing it later.
Casting to a different storage type does not fix the fact that you reversed the storage types to begin with :) It just makes a warning go away, which was trying to tell you something.
If the code is reversed (as it should be), free()
will work as expected since you can actually modify the memory that you allocated.
Solution 3:
It makes no sense to malloc a pointer to const, since you will not be able to modify its contents (without ugly hacks).
FWIW though, gcc just gives a warning for the following:
//
// const.c
//
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *p = malloc(100);
free(p);
return 0;
}
$ gcc -Wall const.c -o const
const.c: In function ‘main’:
const.c:8: warning: passing argument 1 of ‘free’ discards qualifiers from pointer target type
$
What compiler are you using ?
Solution 4:
There are cases you want to free a const*
. However you don't want to do it unless you allocate/asign it in the same function. Else you are likely to break things. See the code below for a real world example. I use const
in the function declarations to show that I am not changing the content of the arguments. However it is reassigned with a lowercased duplicate (strdup) that needs to be freed.
char* tolowerstring(const char *to_lower)
{
char* workstring = strdup(to_lower);
for(;workstring != '\0'; workstring++)
*workstring = tolower(workstring);
return workstring;
}
int extension_checker(const char* extension, const char* to_check)
{
char* tail = tolowerstring(to_check);
extension = tolowerstring(extension);
while ( (tail = strstr( tail+1, extension)) ) { /* The +1 prevents infinite loop on multiple matches */
if ( (*extension != '.' ) && ( tail[-1] != '.'))
continue;
if ( tail[strlen(extension)] == '\0') {
free(tail);
free( (char*) extension);
return 1;
}
}
free(tail);
free( (char *) extension);
return 0;
}