通过将其传递给库文件来分配和释放指针? [英] allocate and free pointer by passing it to library files?
本文介绍了通过将其传递给库文件来分配和释放指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将内存分配给main函数中的指针,然后将其传递给库函数并在库函数中将其释放.
如何实现呢?
请帮助我....
I want to allocate memory to a pointer in main function, then pass it to a library function and free it there in the library function.
How to implement this?
Please help me....
推荐答案
^ ]
与您所说的完全一样.
你有没有尝试过?您遇到了什么问题?
Exactly like you said it.
have you tried? what problem did you faced?
就像琼所说的那样,
使用指针的引用(或指向上方)是安全的
对于它们的堆栈传递(在同一进程的任何(解除)分配上下文中):):
As Joan said,
it is safe to use the referencing (or overpointing) of the pointers
for their stack passing (in any (de-)allocation contexts of the same process) :) :
extern void lib_alloc(int** ppInt);
//{
// if (ppInt) {
// *ppInt = new int[2];
// }
//}
extern void lib_free(int** ppInt);
//{
// if (ppInt) {
// delete *ppInt;
// *ppInt = NULL;
// }
//}
void loc_test()
{
int* pInt(NULL);
lib_alloc(&pInt);
lib_free(&pInt);
}
这篇关于通过将其传递给库文件来分配和释放指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文