【问题标题】:c - freeing dynamically allocated memory for a variable that is rerturned from a functionc - 为从函数返回的变量释放动态分配的内存
【发布时间】:2018-07-29 18:20:28
【问题描述】:

我在下面编写了一个函数来格式化字符串。它删除空格和字符'-'。然后在每 3 个字符后插入一个空格。我在函数中有malloc() 两个字符串,但在退出函数之前我只有free() 其中一个。我认为这是内存泄漏的一个例子。有没有办法更好地设计这个功能,以免像这样泄漏内存?

#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);
}

【问题讨论】:

  • 函数中,string_with_spaces包含了malloc()给出的地址。返回到 main() 时,该地址被存储到 string_returned 中。所以你仍然有可能在 printf() 调用之后 free() 它。
  • 1) for (i=0; i &lt;= strlen(string_with_spaces); i++) { 您的索引超出了字符串的大小,并且 2) strlen(string_with_spaces) 并非所有字符串都是以空结尾的。
  • 3) s[12] = "ABC-3-26--54"; 12 这里太短了;你需要一个额外的 '\0' 终结者

标签: c memory heap-memory free


【解决方案1】:

每个分配的对象都有一个负责释放它的所有者。当您从函数返回这样的对象时,调用者将成为所有者。然后调用者负责释放它或将所有权传递给其他函数(或数据)。

在您的情况下,main 是在FormatString 中分配的字符串的新所有者,因此main 应该在完成后释放它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2017-01-10
    • 1970-01-01
    • 2012-07-29
    • 2017-06-13
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多