realloc() 为recv() 中使用的缓冲区分配内存
我需要从套接字接收数据并将其存储到缓冲区中,但我需要确保获取所有数据,以便我将数据放入循环中。因此,为了确保缓冲区中的空间不会耗尽,我尝试使用 realloc 来调整分配给缓冲区的内存大小。到目前为止,我已经:
// receive response
int i = 0;
int amntRecvd = 0;
char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
i += amntRecvd;
realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
}
然而,这似乎无法正常工作,因为 Valgrind 抱怨“valgrind:‘不可能’发生了:”。关于如何正确完成此操作有什么建议吗?
谢谢, Hristo
更新...我意识到我错误地使用了 realloc。这是修订版:
int i = 0;
int amntRecvd = 0;
char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
i += amntRecvd;
char *temp = realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
if (temp != NULL) {
pageContentBuffer = temp;
}
}
然而,valgrind 仍然在抱怨:
==25812== Syscall param socketcall.recvfrom(buf) points to unaddressable byte(s)
==25812== at 0x33B880DAA1: recv (in /lib64/libpthread-2.5.so)
==25812== by 0x401D78: tunnelURL (proxy.c:371)
==25812== by 0x40142A: client_thread (proxy.c:194)
==25812== by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==25812== by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)
==25812== Address 0x5642768 is 0 bytes after a block of size 4,104 alloc'd
==25812== at 0x4A0590B: realloc (vg_replace_malloc.c:306)
==25812== by 0x401D47: tunnelURL (proxy.c:373)
==25812== by 0x40142A: client_thread (proxy.c:194)
==25812== by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==25812== by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)
I need to recv() data from a socket and store it into a buffer, but I need to make sure get all of the data so I have things in a loop. So to makes sure I don't run out of room in my buffer, I'm trying to use realloc to resize the memory allocated to the buffer. So far I have:
// receive response
int i = 0;
int amntRecvd = 0;
char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
i += amntRecvd;
realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
}
However, this doesn't seem to be working properly since Valgrind is complaining "valgrind: the 'impossible' happened:". Any advice as to how this should be done properly?
Thanks,
Hristo
update... I realized I was using realloc incorrectly. Here is a revised version:
int i = 0;
int amntRecvd = 0;
char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
i += amntRecvd;
char *temp = realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
if (temp != NULL) {
pageContentBuffer = temp;
}
}
However, valgrind still is complaining:
==25812== Syscall param socketcall.recvfrom(buf) points to unaddressable byte(s)
==25812== at 0x33B880DAA1: recv (in /lib64/libpthread-2.5.so)
==25812== by 0x401D78: tunnelURL (proxy.c:371)
==25812== by 0x40142A: client_thread (proxy.c:194)
==25812== by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==25812== by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)
==25812== Address 0x5642768 is 0 bytes after a block of size 4,104 alloc'd
==25812== at 0x4A0590B: realloc (vg_replace_malloc.c:306)
==25812== by 0x401D47: tunnelURL (proxy.c:373)
==25812== by 0x40142A: client_thread (proxy.c:194)
==25812== by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==25812== by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了@whirlwind所说的之外,还有第二个问题:
sizeof
不返回之前分配的内存量,它实际上是一个编译时构造,相当于sizeof(char * )
,即字符指针的大小。您需要在变量中手动跟踪缓冲区的长度。没有标准的方法来“询问”malloc/realloc 分配了多少内存。
Aside from what @whirlwind said, there's also a second issue:
sizeof
does not return the amount of memory previously allocated, it is actually a compile-time construct which is equivalent tosizeof(char *)
, i.e. the size of a character pointer.You will need to keep track of the length of your buffer manually in a variable. There is no standard way to "ask" how much memory has been allocated by malloc/realloc.
也许存在问题是因为您滥用了 realloc()。您需要查看它是否返回一个新指针,如果是,则存储该指针。
Perhaps there is an issue because you are misusing realloc(). You need to see if it returns a new pointer, and if so, store that pointer.
查找 realloc。
sizeof
是编译时值,而不是运行时值。realloc
有可能返回 0。试试这个...
最好的选择是重新分配、复制,然后显式释放内存 -
realloc
很奇怪。Look up realloc.
sizeof
is a compile time value, not runtime.It is possible for
realloc
to return 0.Try this...
Your best bet is to reallocate, copy and then free the memory explicitly --
realloc
is quirky.您的主要问题是您重新分配了错误的内存量。您希望
sizeof(pageContentBuffer)
只是sizeof(char *)
,这意味着您重新分配的空间远远少于第二次读取所需的空间。Your main problem is that you're reallocing the wrong amount of memory. You want
sizeof(pageContentBuffer)
is justsizeof(char *)
, which means you're reallocing far less than you need for the second read.