Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Allocating memory

Allocating memory

Scheduled Pinned Locked Moved C / C++ / MFC
jsonperformancequestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ALLERSLIT
    wrote on last edited by
    #1

    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.

    E E L 3 Replies Last reply
    0
    • A ALLERSLIT

      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.

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      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:

      1 Reply Last reply
      0
      • A ALLERSLIT

        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.

        E Offline
        E Offline
        elchupathingy
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A ALLERSLIT

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          A 1 Reply Last reply
          0
          • L Lost User

            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.

            A Offline
            A Offline
            ALLERSLIT
            wrote on last edited by
            #5

            Thanks for all answers and thanks to Richard, that will sure do the job for me!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups