Does “sendmsg” free memory of the buffer or msg? [closed]

久未见 提交于 2021-02-08 12:14:35

问题


Does sendmsg free memory of the buffer or msg?

Please guide me on this.


回答1:


No, sendmsg() does not free the passed-in memory. It cannot possibly do so, because that memory may not even have come from malloc(). You can free() the memory any time after calling sendmsg(), as the system will have already made the necessary copies.




回答2:


No you cannot do free it. It just sends out bytes of memory making using of msghdr structure.

Usually you allocate memory write into iovec of msghdr and call sendmsg to transfer it as,

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


来源:https://stackoverflow.com/questions/21831121/does-sendmsg-free-memory-of-the-buffer-or-msg

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!