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