【问题标题】:Does "sendmsg" free memory of the buffer or msg? [closed]“sendmsg”是否释放缓冲区或 msg 的内存? [关闭]
【发布时间】:2014-03-16 20:52:05
【问题描述】:

sendmsg 是否释放缓冲区或 msg 的内存?

请指导我。

【问题讨论】:

  • 你的问题没有任何意义!
  • 您将不得不阅读文档。
  • 你有什么代码可以配合你的尝试吗?
  • 阅读手册页,Google 并搜索。就像我在 [这里] 找到的一样。(stackoverflow.com/a/4259888/1272394)

标签: c linux memory


【解决方案1】:

不,你不能释放它。它只是使用msghdr 结构发送内存字节。

通常你将内存写入分配到msghdr的iovec并调用sendmsg将其传输为,

char buffer[SIZE]="DATA";  // Data to send into buffer
struct iovec io;           // Segment which will store outgoing message
struct msghdr msgh;        // msghdr structure 
...
io.iov_base = buffer;      // Specify the components of the message in an iovec
io.iov_len = SIZE;
msgh.msg_iov = &io;  
... 
sendmsg(fd,&msgh,0);       // send msg which just send msg in a iovec buffer

【讨论】:

  • 谢谢。我现在明白了。再次感谢。
【解决方案2】:

不,sendmsg() 不会释放传入的内存。它不可能这样做,因为该内存甚至可能不是来自malloc()。您可以在调用sendmsg() 后随时free() 内存,因为系统已经制作了必要的副本。

【讨论】:

  • 出于某种原因,我觉得“......内存可能甚至不是来自 malloc()”这行很幽默。
猜你喜欢
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2014-12-22
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多