Home > database >  How to free memory following a '\0' placed somewhere in a string in C?
How to free memory following a '\0' placed somewhere in a string in C?

Time:11-08

I've written a very minimal whitespace trimming function in C that just copies the valid characters into a duplicate and puts a '\0' at the end.

char *trim(char *line) {
    char *out = strdup(line);

    // copy valid chars to duplicate
    char *op = out;
    char *ip = line;
    while (*ip != '\0') {
        if (!isspace(*ip)) {
            *op = *ip;
            op  ;
        }
        ip  ;
    }
    // put second '\0' in string, although more memory is dedicated to it
    *op = '\0';

    return out;
}

This means that calling this function with something like {'F', ' ', 'o', ' ', ' ', 'o', '\0'} should return me the string {'F', 'o', 'o', '\0', ' ', 'o', '\0'}.

This seems pretty inefficient. I'd rather return: {'F', 'o', 'o', '\0'}.

Questions

  1. What does it mean to have multiple occurences of \0 in a string? It started behaving unintiutively when I duplicated the string with strdup() and then checked it's length again with strlen().
  2. How do I free the memory after the first \0 such that it is not dedicated to the string anymore?

CodePudding user response:

  1. How do I free the memory after the first \0 such that it is not dedicated to the string anymore?

You can do this with:

char *NewSpace = realloc(out, strlen(out)   1);
if (NewSpace)
    out = NewSpace;

Testing NewSpace is a guard against realloc failing, in which case it returns a null pointer and the old memory remains allocated. (realloc should not fail when shortening an allocation, but it is good practice to guard against problems.)

This can be done only with dynamically allocated memory. Your code shows the space for out was allocated with strdup, so that is dynamically allocated memory.

Note that memory allocation routines commonly work in units of 16 bytes or something along those lines, so reallocating 7 bytes to 4 will not have any effect on memory use. Reallocating ten mebibytes to one might release the memory. (This is not guaranteed by the C standard, and some implementations might choose not to.)

  1. What does it mean to have multiple [occurrences] of \0 in a string?

It is not possible to have multiple occurrences of a null character in a string, because a string is defined to be a sequence of characters that ends with the first null character.

You can have an array with multiple null characters in it. An array is a sequence of elements of the same type, and its elements can have any values of that type, including multiple zeros. Arrays are often used to hold strings. When an array, or part of it, is used to hold a string, the string will simply end at the first null character. Remaining elements in the array will not be used by functions operating on it as a string.

In C, an array has a fixed amount of memory. (For a so-called “variable length array,” the length is fixed when the array is created. It may vary in different instances of code execution reaching the array definition, but it does not vary in a particular instance.) In large part, the array is the memory, and that does not change according to the data in it. In contrast, a string is defined by its contents; it ends at its first null character.

  •  Tags:  
  • c
  • Related