Can I use free for const char*? Will this cause any problems?
Can I use free for const char* ? Will this cause any problems?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. By it’s nature,
free()needs the freedom to write into the given memory, to do “book keeping”. This is why it’s defined to take a non-constpointer.As others have pointed out, this doesn’t mean that it can’t work; C can drop the
const-ness of a pointer and let the function run as if it was called withoutconst. The compiler will warn when that happens though, which is why I consider it to “cause problems”.