简体   繁体   中英

c - freeing dynamically allocated memory for a variable that is rerturned from a function

I've written a function below which formats a string. It removes spaces and the char '-'. It then inserts a space after every 3 characters. I malloc() two strings in the function but I only free() one of them before I exit the function. I presume this is an example of memory leaking. Is there a way to better design this function so as to not leak memory like this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* FormatString(char* s) {

    /*Get string length*/
    int original_str_length = strlen(s);
    int num_elements_to_delete = 0;
    int i=0;
    int j=0;

    /*count chars '-' and ' ' to delete*/
    for (int i=0; i <= original_str_length; i++) {

        if((s[i] == '-') || (s[i] == ' ')) {
            num_elements_to_delete++;
        }
    }

    /*allocate new string to hold string minus '-' and ' '*/
    char* string_minus_chars = (char *) malloc(original_str_length - num_elements_to_delete);

    for (i=0; i <= original_str_length; i++) {

        /*if char is not '-' or ' ' copy to new string*/
        if((s[i] != '-') && (s[i] != ' ')) {

            string_minus_chars[j] = s[i];
            j++;
        }
    }

    /*Print the string*/
    printf("String minus chars is %s\r\n", string_minus_chars);
    int string_minus_chars_length = strlen(string_minus_chars);

    printf("string_minus_chars_length %d\r\n", string_minus_chars_length);

    int extra_spaces = (string_minus_chars_length) / 3;

    printf("Number of spaces needed is %d\r\n", extra_spaces);

    /*allocate new string with spaces*/
    char* string_with_spaces = (char *) malloc(sizeof(char)*(extra_spaces + string_minus_chars_length));

    int space_counter = 0;
    int index = 0;

    for (i=0; i <= strlen(string_with_spaces); i++) {

         if(space_counter == 3) {
             printf("str2 index is %d, str1 index is %d,space counter is %d, inserting space\r\n",i, index, space_counter);
             string_with_spaces[i] = ' ';
             space_counter = 0;

         }
         else {
             printf("str2 index is %d,str1 index is %d,space counter is %d,  copying %c\r\n", i, index,space_counter, string_minus_chars[index]);
             string_with_spaces[i] = string_minus_chars[index];
         space_counter++;
         index++;
         }

      }

    printf("String_with_spaces is:%s\r\n", string_with_spaces);

    free(string_minus_chars);
    return string_with_spaces;

}


int main(void) {

    char s[12] = "ABC-3-26--54";
    char* string_returned;

    string_returned = FormatString(s);

    printf("String returned is %s",string_returned);
}

Every allocated object has an owner responsible for freeing it. When you return such an object from a function, the caller becomes the owner. It is then the caller's responsibility to either free it or pass the ownership to some other function (or data).

In your case, main is the new owner of the string allocated in FormatString , so main should free it when it's done with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM