相关疑难解决方法(0)

返回函数前自由分配内存

我试图在函数中使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

c malloc free pointers

10
推荐指数
4
解决办法
2万
查看次数

标签 统计

c ×1

free ×1

malloc ×1

pointers ×1