realloc() 为recv() 中使用的缓冲区分配内存

发布于 2024-08-29 10:50:22 字数 1727 浏览 7 评论 0原文

我需要从套接字接收数据并将其存储到缓冲区中,但我需要确保获取所有数据,以便我将数据放入循环中。因此,为了确保缓冲区中的空间不会耗尽,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生死何惧 2024-09-05 10:50:24

除了@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 to sizeof(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.

皇甫轩 2024-09-05 10:50:24

也许存在问题是因为您滥用了 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;
    pageContentBuffer = realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
}

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.

// 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;
    pageContentBuffer = realloc(pageContentBuffer, 4096 + sizeof(pageContentBuffer));
}
ら栖息 2024-09-05 10:50:24

查找 realloc

sizeof 是编译时值,而不是运行时值。

realloc 有可能返回 0。

试试这个...

// receive response
int i = 0;
int amntRecvd = 0;
int currentSize = 4096;
int oldSize = currentSize;
char *pageContentBuffer = (char*) malloc(currentSize);
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
    i += amntRecvd;
    oldSize = currentSize; 
    currentSize += 4096; 
    char *newBuffer = malloc(currentSize); 
    memcpy(newBuffer,pageContentBuffer,oldSize); 
    free(pageContentBuffer); 
    pageContentBuffer = newBuffer;
}

最好的选择是重新分配、复制,然后显式释放内存 - realloc 很奇怪。

Look up realloc.

sizeof is a compile time value, not runtime.

It is possible for realloc to return 0.

Try this...

// receive response
int i = 0;
int amntRecvd = 0;
int currentSize = 4096;
int oldSize = currentSize;
char *pageContentBuffer = (char*) malloc(currentSize);
while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
    i += amntRecvd;
    oldSize = currentSize; 
    currentSize += 4096; 
    char *newBuffer = malloc(currentSize); 
    memcpy(newBuffer,pageContentBuffer,oldSize); 
    free(pageContentBuffer); 
    pageContentBuffer = newBuffer;
}

Your best bet is to reallocate, copy and then free the memory explicitly -- realloc is quirky.

贵在坚持 2024-09-05 10:50:24

您的主要问题是您重新分配了错误的内存量。您希望

realloc(pageContentBuffer, 4096 + i);

sizeof(pageContentBuffer) 只是 sizeof(char *),这意味着您重新分配的空间远远少于第二次读取所需的空间。

Your main problem is that you're reallocing the wrong amount of memory. You want

realloc(pageContentBuffer, 4096 + i);

sizeof(pageContentBuffer) is just sizeof(char *), which means you're reallocing far less than you need for the second read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文