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. Resizing a dynamic pointer array

Resizing a dynamic pointer array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 3 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
    aquawicket
    wrote on last edited by
    #1

    I am using a dynamic array that I often need to resize. I've tried a few way to resize it and so far this is what works. But their MUST be a better way. Ideas? int *n = NULL; // Pointer to int, initialize to nothing. n = new int[2]; // allocate needed room ///////////////////////////////////// /// Time to make more room!! int *temp = NULL; //Create a temp :( temp = new temp[4]; //allocate for(register int i=0; i<4; i++){ //transfer data to temp temp[i] = n[i]; } delete n; //delete old n = new int[4]; //make new one for(register int i=0; i<4; i++){ //transfer the data back n[i] = temp[i]; } delete temp; //delete temp

    K J 2 Replies Last reply
    0
    • A aquawicket

      I am using a dynamic array that I often need to resize. I've tried a few way to resize it and so far this is what works. But their MUST be a better way. Ideas? int *n = NULL; // Pointer to int, initialize to nothing. n = new int[2]; // allocate needed room ///////////////////////////////////// /// Time to make more room!! int *temp = NULL; //Create a temp :( temp = new temp[4]; //allocate for(register int i=0; i<4; i++){ //transfer data to temp temp[i] = n[i]; } delete n; //delete old n = new int[4]; //make new one for(register int i=0; i<4; i++){ //transfer the data back n[i] = temp[i]; } delete temp; //delete temp

      K Offline
      K Offline
      Karismatic
      wrote on last edited by
      #2

      why don't you use vector in this case you need not to worry about resizing it grows automatically int the above question when you delete temp write delete[] temp as it is an array the above code leads to memory leakage

      Regards, Pankaj Sachdeva "There is no future lies in any job" "but" "future lies in the person who holds the job"

      1 Reply Last reply
      0
      • A aquawicket

        I am using a dynamic array that I often need to resize. I've tried a few way to resize it and so far this is what works. But their MUST be a better way. Ideas? int *n = NULL; // Pointer to int, initialize to nothing. n = new int[2]; // allocate needed room ///////////////////////////////////// /// Time to make more room!! int *temp = NULL; //Create a temp :( temp = new temp[4]; //allocate for(register int i=0; i<4; i++){ //transfer data to temp temp[i] = n[i]; } delete n; //delete old n = new int[4]; //make new one for(register int i=0; i<4; i++){ //transfer the data back n[i] = temp[i]; } delete temp; //delete temp

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

        I can barely begin to tell you what is wrong with that. 1) Use ‘vector’ and all your problems will go away. 2) If you must provided your own (do not double copy the data): 2.1) Allocate the new amount of space required. 2.2) Copy the old data to the new data space. 2.3) Free the old space. 2.4) Assign the pointer to the new space. In C you would have another option (realloc) that if it failed, you would have to resort to (2) above, but I do not know a way to do that in C++. P.S. delete[] n;

        INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

        A 1 Reply Last reply
        0
        • J John R Shaw

          I can barely begin to tell you what is wrong with that. 1) Use ‘vector’ and all your problems will go away. 2) If you must provided your own (do not double copy the data): 2.1) Allocate the new amount of space required. 2.2) Copy the old data to the new data space. 2.3) Free the old space. 2.4) Assign the pointer to the new space. In C you would have another option (realloc) that if it failed, you would have to resort to (2) above, but I do not know a way to do that in C++. P.S. delete[] n;

          INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

          A Offline
          A Offline
          aquawicket
          wrote on last edited by
          #4

          I think I will use vector. But just to check, here is my own way revised. number *n - NULL; //create n = new number[2]; //allocate ////We need more room number *temp = NULL; //create temp temp = new number[4]; //allocate for(register int i=0; i<4; i++){ //transfer old into temp temp[i] = n[i]; } delete[] n; //delete old n = temp; // point to new data

          J 1 Reply Last reply
          0
          • A aquawicket

            I think I will use vector. But just to check, here is my own way revised. number *n - NULL; //create n = new number[2]; //allocate ////We need more room number *temp = NULL; //create temp temp = new number[4]; //allocate for(register int i=0; i<4; i++){ //transfer old into temp temp[i] = n[i]; } delete[] n; //delete old n = temp; // point to new data

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

            Yes, that is the idea; short and simple. The only thing I would change in the example is the assignment to NULL, because you immediately assign a value to it after the declaration. number* n = new number[2]; // declare and assign in one shot number* temp = new number[4]; Those are shorter and make more since to me. P.S. The register keyword is unneeded, because most modern compilers are better at picking register variables than we are and it is free to ignore the suggestion, which is all that keyword is. Good Luck!

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            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