简体   繁体   English

如果指针重定向,则释放可用内存

[英]free memory if pointer redirects

I've some trouble understanding how to free my memory correctly in following codesnippet: 我在理解以下代码片段中如何正确释放内存方面遇到一些麻烦:

char *func(char* s){

   /* do something with s */


   // pass s to function, create new string, return this string
   s = function_allocating_mem(s);

   // pass s to function, create new string, return this string
   s = other_function_allocation_mem(s);
   /* do other stuff again */

   return s;
}

int main(void){
    char str[] = "somestring";
    str = func(str);
}

Now I allocated different sizes of memory two times. 现在,我两次分配了不同大小的内存。 But if I get it right, I just change the pointer address and never free the memory. 但是,如果我做对了,我只会更改指针地址,而永远不会释放内存。

I don't really know, what to google to find an example 我真的不知道,谷歌怎么找到一个例子

Is that correct and how would I change it? 那是正确的,我将如何更改?

EDIT: I removed the second parameter of the function. 编辑:我删除了该函数的第二个参数。 It was not necessary and confusing. 这没有必要并且令人困惑。

In code You've provided, correct way to free memory would be: 在您提供的代码中,释放内存的正确方法是:

char *s2;
s2 = other_function_allocation_mem(s);
free( s );
s = s2;
...
free( s );
return ns;

When you allocate memory from the heap in your program, you have to have a clear understanding of: 从程序中的堆分配内存时,您必须对以下内容有清楚的了解:

  1. Where memory from heap is allocated in your program. 程序中从堆中分配内存的位置。

  2. How the ownership of heap allocated memory is transferred from one function to the next, and 堆分配的内存的所有权如何从一个函数转移到另一个函数,以及

  3. Where it is deallocated before the program ends. 程序结束前将其释放的位置。

In your case, assuming function_allocating_mem and other_function_allocation_mem don't call free on the input argument, you have to make sure that the memory allocated in those functions is deallocated in either fun or main . 在您的情况下,假设function_allocating_memother_function_allocation_mem不对输入参数进行free调用,则必须确保在这些函数中分配的内存以funmain释放。

char *func(char* s, const char* os){
   char* s1 = NULL;
   char* s2 = NULL;
   /* do something with s and os */


   // pass s to function, create new string, return this string
   s1 = function_allocating_mem(s);

   // pass s to function, create new string, return this string
   s2 = other_function_allocation_mem(s1);

   /* do other stuff again */

   // Deallocate memory that was allocated by function_allocating_mem().
   free(s1);


   // Memmory allocated by other_function_allocation_mem is returned to the 
   // calling function.
   return s2;
}

int main(void){
    char str[] = "somestring";

    // This is not legal anyway.
    // str = func(str, "some other string");

    char* s = fun(str);

    // Use s ...

    // Before returning from this function, deallocate memory
    // that was allocated in the call to fun().
    free(s);
}

So many problems. 这么多问题。

1. You declared that func takes two parameters. 1.您声明 func有两个参数。 Then you only called it with one param. 然后,您仅用一个参数进行调用。
2. You declared that func returns a char* , then you try to assign that to a char[] . 2.您声明 func返回 char* ,然后尝试将其分配给 char[]
Arrays and pointers are related, but they are not interchangable. 数组和指针是相关的,但它们不可互换。
3. In func , you never use param os . 3.在 func ,您永远不会使用param os
4. You return variable ns . 4.您返回变量ns It was never declared. 从来没有宣布过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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