返回函数前释放已分配的内存

时间:2021-02-27 21:17:38

I am trying to return an array using malloc in a function:

我试图在函数中使用malloc返回一个数组:

char* queueBulkDequeue(queueADT queue, unsigned int size)
{
    unsigned int i;
    char* pElements=(char*)malloc(size * sizeof(char));
    for (i=0; i<size; i++)
    {
        *(pElements+i) = queueDequeue(queue);
    }
    return pElements;
}

The problem is that I need to free it because my MCU's heap size is limited. But I want to return it so I cannot free it in the function, right?. Can I free the allocated memory outside the function (where I call the function). Is there any best practices for this? Thank you in advance!

问题是我需要释放它,因为我的MCU堆大小有限。但是我想要退回它,所以我不能在功能中释放它,对吧?我可以在函数外部释放已分配的内存(我称之为函数)。这有什么最好的做法吗?先谢谢你!

4 个解决方案

#1


9  

As the memory allocated by malloc() is on the heap and not on the stack, you can access it regardless of which function are you in. If you want to pass around malloc()'ed memory, you have literally no other option than freeing it from the caller. (in reference counting terms, that's what is called an ownership transfer.)

由malloc()分配的内存在堆上而不在堆栈上,无论你使用哪个函数,都可以访问它。如果你想传递malloc()的内存,你几乎没有别的选择。从呼叫者那里解放出来。 (在参考计算术语中,这就是所谓的所有权转移。)

#2


10  

1) Yes, you can free() the malloc'ed memory outside the function

1)是的,你可以释放()函数外的malloc内存

2) No, you cannot free it inside the function and have the data passed outside the function, so you must do 1) here

2)不,你不能在函数内释放它并将数据传递到函数外部,所以你必须在这里做1)

3) If you're concerned about scarce memory, you need to check for failure from memory allocations always, which you fail to do here, which is then likely to lead to a segfault

3)如果你担心内存不足,你需要始终检查内存分配的失败,这是你在这里做不到的,这可能会导致段错误

#3


8  

Ofcourse you can free the memory allocated in a function outside of that function provided you return it.

当然,如果你返回它,你可以释放在该函数之外的函数中分配的内存。

But, an alternative would be to modify your function like below, where the caller only allocates & frees the memory. This will be inline with concept of the function which allocates the memory takes responsibility for freeing the memory.

但是,另一种方法是修改你的函数,如下所示,调用者只分配和释放内存。这将与分配内存的函数的概念内联,负责释放内存。

void queueBulkDequeue(queueADT queue, char *pElements, unsigned int size) 
{     
   unsigned int i;     
   for (i=0; i<size; i++)     
   {         
      *(pElements+i) = queueDequeue(queue);     
   }     
   return; 
} 

//In the caller

//在来电者中

char *pElements = malloc(size * sizeof(char));
queueBulkDequeue(queue, pElements, size);
//Use pElements
free(pElements);

#4


4  

Yes, you can free memory allocated in a function that you call outside the function; this is precisely what you need to do in this case.

是的,您可以释放在函数外部调用的函数中分配的内存;这正是你在这种情况下需要做的。

Alternatives include passing a buffer and its length into the function, and returning the actual length to the caller, the way fgets does. This may not be the best alternative, because the callers would need to call your function in a loop.

替代方案包括将缓冲区及其长度传递给函数,并将实际长度返回给调用者,就像fgets那样。这可能不是最佳选择,因为调用者需要在循环中调用您的函数。

#1


9  

As the memory allocated by malloc() is on the heap and not on the stack, you can access it regardless of which function are you in. If you want to pass around malloc()'ed memory, you have literally no other option than freeing it from the caller. (in reference counting terms, that's what is called an ownership transfer.)

由malloc()分配的内存在堆上而不在堆栈上,无论你使用哪个函数,都可以访问它。如果你想传递malloc()的内存,你几乎没有别的选择。从呼叫者那里解放出来。 (在参考计算术语中,这就是所谓的所有权转移。)

#2


10  

1) Yes, you can free() the malloc'ed memory outside the function

1)是的,你可以释放()函数外的malloc内存

2) No, you cannot free it inside the function and have the data passed outside the function, so you must do 1) here

2)不,你不能在函数内释放它并将数据传递到函数外部,所以你必须在这里做1)

3) If you're concerned about scarce memory, you need to check for failure from memory allocations always, which you fail to do here, which is then likely to lead to a segfault

3)如果你担心内存不足,你需要始终检查内存分配的失败,这是你在这里做不到的,这可能会导致段错误

#3


8  

Ofcourse you can free the memory allocated in a function outside of that function provided you return it.

当然,如果你返回它,你可以释放在该函数之外的函数中分配的内存。

But, an alternative would be to modify your function like below, where the caller only allocates & frees the memory. This will be inline with concept of the function which allocates the memory takes responsibility for freeing the memory.

但是,另一种方法是修改你的函数,如下所示,调用者只分配和释放内存。这将与分配内存的函数的概念内联,负责释放内存。

void queueBulkDequeue(queueADT queue, char *pElements, unsigned int size) 
{     
   unsigned int i;     
   for (i=0; i<size; i++)     
   {         
      *(pElements+i) = queueDequeue(queue);     
   }     
   return; 
} 

//In the caller

//在来电者中

char *pElements = malloc(size * sizeof(char));
queueBulkDequeue(queue, pElements, size);
//Use pElements
free(pElements);

#4


4  

Yes, you can free memory allocated in a function that you call outside the function; this is precisely what you need to do in this case.

是的,您可以释放在函数外部调用的函数中分配的内存;这正是你在这种情况下需要做的。

Alternatives include passing a buffer and its length into the function, and returning the actual length to the caller, the way fgets does. This may not be the best alternative, because the callers would need to call your function in a loop.

替代方案包括将缓冲区及其长度传递给函数,并将实际长度返回给调用者,就像fgets那样。这可能不是最佳选择,因为调用者需要在循环中调用您的函数。