我正在尝试在一个函数中使用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;
}
问题是我需要释放它,因为我的MCU堆大小是有限的。但我想要返回它,所以我不能在函数中释放它,对吗?那么我能否在函数外部(调用函数的地方)释放已分配的内存?这方面是否有最佳实践?提前谢谢!
malloc
的全部意义所在。如果不能做到这一点,那么就没有理由创建malloc
了。 - R.. GitHub STOP HELPING ICE