Allocating memory
-
Hey, this is my code:
char szBuffer\[1024\]; recv(sock,szBuffer,sizeof(szBuffer),NULL); printf("%s", szBuffer);
now the size of the websites vary, how would i allocate just enough memory for the actual content so the rest of the buffer wont be filled with junk ? Thanks.
-
Hey, this is my code:
char szBuffer\[1024\]; recv(sock,szBuffer,sizeof(szBuffer),NULL); printf("%s", szBuffer);
now the size of the websites vary, how would i allocate just enough memory for the actual content so the rest of the buffer wont be filled with junk ? Thanks.
Good question ... without an answer. The physics has its limits, and computer aren't paragnostic. You have only two chances: know the size of what you are receiving in advance (and allocate the required) or, "guess it" and store as you can. You may waste something, or you may be required to relocate. In both of the cases, dynamic allocation must be used (so an on-stack array cannot help). There are dynamic memory allocation operations (search for "new and delete") or dynamic allocating classes (see std::vector). Search for their documentation.
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
Hey, this is my code:
char szBuffer\[1024\]; recv(sock,szBuffer,sizeof(szBuffer),NULL); printf("%s", szBuffer);
now the size of the websites vary, how would i allocate just enough memory for the actual content so the rest of the buffer wont be filled with junk ? Thanks.
Depending on how you are getting the content the answer is different. Reading from a file is simple. But, if you are getting the website content from a web server then all you have to do is parse the HTTP return headers for the Content-Length header option. This will give you the size of the buffer that is required. From here you can make the new buffer and store the rest of the web page into the newly allocated buffer, or you can take the size and resend the same request after finding out the required buffer size. Skipping the headers by looking for \r\n\r\n in the buffer and once this is found to begin storing the website contents. So, in both cases you need to dynamically allocate the buffer as stated before, but it all depends on how you are reading the website content, from file or from a web server.
-
Hey, this is my code:
char szBuffer\[1024\]; recv(sock,szBuffer,sizeof(szBuffer),NULL); printf("%s", szBuffer);
now the size of the websites vary, how would i allocate just enough memory for the actual content so the rest of the buffer wont be filled with junk ? Thanks.
See the remarks section here[^], you should always check the return values of system calls to see whether they succeeded or not. In the case of
recv
it will tell you if the message cannot be completely transferred to the buffer. You should re-allocate your buffer to a larger size and try again until you are able to read the entire message.It's time for a new signature.
-
See the remarks section here[^], you should always check the return values of system calls to see whether they succeeded or not. In the case of
recv
it will tell you if the message cannot be completely transferred to the buffer. You should re-allocate your buffer to a larger size and try again until you are able to read the entire message.It's time for a new signature.