const char* and free()
Asked Answered
M

3

6

Given the next code example, I'm unable to free the parameter const char* expression:

// removes whitespace from a characterarray
char* removewhitespace(const char* expression, int length)
{
    int i = 0, j = 0;
    char* filtered;
    filtered = (char*)malloc(sizeof(char) * length);
    while(*(expression + i) != '\0')
    {
        if(!(*(expression + i) == ' '))
        {
            *(filtered + j) = *(expression + i);
            j++;
        }
        i++;
    }
    filtered[j] = '\0';
    free(expression); //this doesn't seem to work
    return filtered;
}

Before I return this function, I try to free the data in the expression parameter but I can't seem to free it.
I think it is probably because it is a constant, but I learned that a character array in C always should be a constant.

The error message I get is at the line with free(expression) and the message is:
expected void* but argument is of type const char * - compiler error

How do I discard the memory that the data expression contains?

Merely answered 15/6, 2012 at 19:50 Comment(5)
What does 'doesn't work' mean? Compiler error? Runtime error? Daemons coming out of the user's nose?Elm
What do you mean by "doesn't seem to work" and "unable to free"?Kinnard
free(expression); expects void* but argument is of type const char*Merely
Freeing and non malloc'd, realloc'd or calloc'd pointer is undefined behavior.Brita
You try to free expression, but what if had been allocated on the stack, or a string literal? You should let the caller decide what to do with this.Picco
A
9

If it's a constant, that implies that it shouldn't be deleted, because it wasn't allocated: the caller could easily have passed a stack-based array to your method, and freeing it would be a bad thing.

The most common convention is that whatever code allocates some data should free the data. It's really not your routine's responsibility to free that argument since it has no way of knowing if that's even a legitimate thing to do.

Assentor answered 15/6, 2012 at 19:54 Comment(2)
If it is part of the "contract" put up by the function definition (resp. by its documentation), it is ok. But then the caller should be aware of it and it should be very well documented, maybe even with a hint in the function name itself.Tinsley
@Tinsley In general, I sort of agree. The problem is than in this case, the contract implied by const and enforced by the compiler is that expression does not change in any way shape or form. Trying to use documentation to circumvent the language spec is definitely a very bad thing as Ernest says.Bole
T
1

If you really want to have a function like this - taking a heap pointer, freeing it and in spite returning another heap pointer - you should document it very well.

Besides, you shouldn't make expression a const char*, because that doesn't match, as you see. char* should be fine, however.

Tinsley answered 15/6, 2012 at 20:8 Comment(0)
C
1

going on directly straight forward with the question is free((void*)expression); that should be the enough to resolved the compiling error. but, as already others have state you should let who is calling your function to manage the memory

Contentious answered 3/8, 2015 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.