如果我想在函数中分配内存:
char* allocate() {
char *cp = (char*)malloc(10);
...
return cp;
}
我可以在 main()
的 cp
中使用返回的内容吗?以及如何释放 cp
?
最佳答案
can I use the returned content in cp in main()?
是的,你可以。
and how to free cp?
使用
free(cp); // Replace cp with the name of the pointer if you are using it in another function
还有,你should not cast the result of
malloc
(and family) in C
关于c - 我在函数中分配内存,并返回 char*,我应该如何释放它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939725/