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. Redimensioning Arrays

Redimensioning Arrays

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structures
6 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.
  • D Offline
    D Offline
    DiegoValdevino
    wrote on last edited by
    #1

    How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6

    C P 2 Replies Last reply
    0
    • D DiegoValdevino

      How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You have two options. 1/ USe a pointer. Then you can do this:BYTE * pByte = new BYTE[256]; // use it delete [] pByte; pByte = new BYTE[300]; Of course every time you resize, you lose all the contents of the array. 2/ Use vector. Like this: #include vector vecBytes; for (int i = 0; i < 256; ++i) vecBytes.push_back(i); now you have an array of 256 values that can be accessed using [n] notation if you like, or the .at function. But you can also delete from the ends and add new items dynamically at will. If you want to delete from the middle you want list, but beware, list has the problem of slower access to individual fields ( as it's a linked list it must step through to the element you want ) Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

      Sonork ID 100.10002:MeanManOz

      I live in Bob's HungOut now

      1 Reply Last reply
      0
      • D DiegoValdevino

        How can I change the size of an array? i need to change from a zero-size array to a n-bytes array ex: BYTE anything[]; and after some code : redimensionfunction(anything[], length); i am using vc++ 6

        P Offline
        P Offline
        Prem Kumar
        wrote on last edited by
        #3

        Hi use malloc and realloc ex. void *pV = NULL ; ... ... .. //now u want to allocate 100 bytes pV = malloc( 100 ) ; ... .. .. //Now u want to make the same memory 200 bytes. pV = realloc(pV, 200); .. .. //Now u want to make it 50 bytes pV = realloc(pV, 50) ;

        C 1 Reply Last reply
        0
        • P Prem Kumar

          Hi use malloc and realloc ex. void *pV = NULL ; ... ... .. //now u want to allocate 100 bytes pV = malloc( 100 ) ; ... .. .. //Now u want to make the same memory 200 bytes. pV = realloc(pV, 200); .. .. //Now u want to make it 50 bytes pV = realloc(pV, 50) ;

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Do NOT use malloc/realloc if you can at all avoid it. They are C functions, and new/delete is highly preferred, especially for complex types. The best reason for this is the fact that new/delete call constructors/destructors and the C functions do not. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

          Sonork ID 100.10002:MeanManOz

          I live in Bob's HungOut now

          L 1 Reply Last reply
          0
          • C Christian Graus

            Do NOT use malloc/realloc if you can at all avoid it. They are C functions, and new/delete is highly preferred, especially for complex types. The best reason for this is the fact that new/delete call constructors/destructors and the C functions do not. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

            Sonork ID 100.10002:MeanManOz

            I live in Bob's HungOut now

            L Offline
            L Offline
            l a u r e n
            wrote on last edited by
            #5

            i use globalalloc() and globalfree() and globalrealloc() a lot and they work just fine if you remember to free the memory when u finished with it whats the problem with this? --- "every year we invent better idiot proof systems and every year they invent better idiots ... and the linux zealots still aren't being sterilized"

            C 1 Reply Last reply
            0
            • L l a u r e n

              i use globalalloc() and globalfree() and globalrealloc() a lot and they work just fine if you remember to free the memory when u finished with it whats the problem with this? --- "every year we invent better idiot proof systems and every year they invent better idiots ... and the linux zealots still aren't being sterilized"

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              lauren wrote: whats the problem with this? There's no *problem* per se, but if you mix C and C++ memory allocationj calls, you need to be careful not to free something you new'd. You also don't get the benefit of constructors and destructors. If you need to realloc, you should instead use a container like vector. Your code will obviously work, but the way of doing things I am suggesting is *better*. To read Stroustrup on this ( and I've pretty much quoted him, so you don't need to as such ), turn to page 577 of the third edition of the C++ Programming Language. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001

              Sonork ID 100.10002:MeanManOz

              I live in Bob's HungOut now

              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