gpt4 book ai didi

c - 释放指针

转载 作者:太空狗 更新时间:2023-10-29 16:13:14 27 4
gpt4 key购买 nike

这只是一个一般性问题,但例如在 Windows 上,如果我创建一个指向 hostent 结构的指针以与 gethostbyname() 一起使用,我是否必须解除分配该指针的内存还是为我处理的。我假设因为我没有专门调用 malloc 它不是我的工作。谁能为我澄清一下?

谢谢

最佳答案

根据msdn

The memory for the hostent structure returned by the gethostbyaddr and gethostbyname functions is allocated internally by the Winsock DLL from thread local storage. Only a single hostent structure is allocated and used, no matter how many times the gethostbyaddr or gethostbyname functions are called on the thread. The returned hostent structure must be copied to an application buffer if additional calls are to be made to the gethostbyaddr or gethostbyname functions on the same thread. Otherwise, the return value will be overwritten by subsequent gethostbyaddr or gethostbyname calls on the same thread. The internal memory allocated for the returned hostent structure is released by the Winsock DLL when the thread exits.

所以你唯一需要释放它的时候是你将它的内容复制到你分配的内存中

关于c - 释放指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14988498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com