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. reduce size of array

reduce size of array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
18 Posts 10 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.
  • M Mepho

    i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho

    A Offline
    A Offline
    Alexander M
    wrote on last edited by
    #9

    If possible, use stack memory instead of allocating. Then you don't have to care about size. Don't try it, just do it! ;-)

    1 Reply Last reply
    0
    • M Mepho

      i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #10

      Why do you want to decrease the size of the buffer? You know that there are no easy ways to manipulate the size of an array! The only way to change the size of an array is to create a complete new one with the size you want and copy all the items you can from the old array.. As other users pointed out, you can use a std::vector or some other collection class. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

      1 Reply Last reply
      0
      • M Mepho

        i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #11

        Something like:

        LPBYTE lpBuffer2 = new BYTE[1000];
        memcpy(lpBuffer2, lpBuffer, 2 * sizeof(BYTE));
        delete [] lpBuffer2;

        The other way is to not make it too big in the first place. Lastly, even if you ended up just using 2 of the 5000 bytes, that's not a big deal in the overall scheme of things.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        J 1 Reply Last reply
        0
        • X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

          Andrew Kirillov wrote: You can try to use realloc(lpBuffer, new_size) No! The original buffer was allocated with new.

          A Offline
          A Offline
          Alex_Y
          wrote on last edited by
          #12

          I guess Andrew mean to use maalloc(...) then realloc(...) no "new" shoud be used in this case. :)

          1 Reply Last reply
          0
          • S sunit5

            use CByteArray

            S Offline
            S Offline
            sunit5
            wrote on last edited by
            #13

            CByteArray::SetSize()

            1 Reply Last reply
            0
            • D David Crow

              Something like:

              LPBYTE lpBuffer2 = new BYTE[1000];
              memcpy(lpBuffer2, lpBuffer, 2 * sizeof(BYTE));
              delete [] lpBuffer2;

              The other way is to not make it too big in the first place. Lastly, even if you ended up just using 2 of the 5000 bytes, that's not a big deal in the overall scheme of things.


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #14

              Strange. Declare and array of 1000 bytes, then copy 2 bytes from the old array to the new array.:doh: INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

              D 1 Reply Last reply
              0
              • J John R Shaw

                Strange. Declare and array of 1000 bytes, then copy 2 bytes from the old array to the new array.:doh: INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #15

                John R. Shaw wrote: Strange. Indeed, but since the OP did not specify what size he wanted the array "shrunk" to, I just pulled a smaller number from the sky.


                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                J 1 Reply Last reply
                0
                • D David Crow

                  John R. Shaw wrote: Strange. Indeed, but since the OP did not specify what size he wanted the array "shrunk" to, I just pulled a smaller number from the sky.


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #16

                  :-DYeh; I thought about that after the fact. Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                  D 1 Reply Last reply
                  0
                  • J John R Shaw

                    :-DYeh; I thought about that after the fact. Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #17

                    John R. Shaw wrote: Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. Yes, I've done that too, only to be asked shortly afterwards, "What is this new_array_size thing and how do I use it?"


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    1 Reply Last reply
                    0
                    • M Mepho

                      i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho

                      M Offline
                      M Offline
                      Mepho
                      wrote on last edited by
                      #18

                      Ya first of all just like to thank everyone for helping out cant use Cbytes for the application and ya the buffer doesnt get filled by only 2... its filled by user specified items. So ill probably use the memcpy function that DavidCrow suggested, the buffer will be reduced by checking the amount the user filled. Was hoping i didnt need to create another buffer array but seems like best solution is to cpy it so far. Thanks, Mepho

                      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