# Help me with dynamically allocating!

• 02-27-2003
Trauts
Help me with dynamically allocating!
Because you cannot do this:

Code:

```int size = 0; cin >> size; int iArray[size];```
I had to dynamically allocate it like this:

Code:

```        int *dataint;         dataint = new int [size];         int *dataint2;         dataint2 = new int [size];         long int randomvalue = 0;         for (int i=0; i < size; i++)         {                 randomvalue = ((rand()%(size-0)) + 0); //gives a random number between 0 and size                 dataint[i] = dataint2[i] = randomvalue;                 cout << randomvalue << endl;         }         ....................         // Run sort algorithm here         dataint = dataint2;  //this is where I want it to COPY dataint2's data over to         //dataint, but NOT just point to it... because then         //a) I can't use delete [] dataint2; because deleting dataint does that already.         //and         //b) the next time I try to reinintialize it, it is already sorted because dataint         //points to dataint2 at that point.         delete [] dataint;         delete [] dataint2;```
So if size is 5; and it initializes dataint and dataint2 as this:

1
3
2
5
3

then it sorts it:

1
2
3
3
5

then "reinitializes" it:

1 (should be 1)
2 (should be 3)
3 (should be 2)
3 (should be 5)
5 (should be 3)

then "sorts" it, only its not changed because it is already sorted:

1
2
3
3
5

And if I use
*dataint = *dataint2;

and the beginning initialization was:

4
5
7
0
9
7
4
3
2
4

then it reinitializes as this:
4
2
3
4
4
4
5
7
7
9

And the second time:
4
3
4
4
4
4
5
7
7
9
• 02-27-2003
beege31337

int size = 0;
cin >> size;

int *array;
array = new int[size];
• 02-27-2003
Jamsan
delete dataint2 first?, or better set it all to NULL pointers before deleting it
• 02-27-2003
vasanth
if all this is for an application.. use C#.. no hasels of clearing the memory with destructors, delete etc.. Garbage collection is automaticaly done.....

*Thinks*:- "Ohh boy hope more people start using the C# board"
• 02-27-2003
Trauts
I fixed it by doing this:

Code:

`for (i=0; i<size; i++) {dataint[i] = dataint2[i];}`
• 02-27-2003
I would have thought that the loop doing this:

dataint[i] = dataint2[i] = randomvalue;

would have set the values of dataint and dataint2 to the same value so there is no need to try:

dataint = dataint2;

which is trying to assign one array to another, and is a definite no no, or for this:

for (i=0; i<size; i++) {dataint[i] = dataint2[i];}

although this is the correct way to assign the values of one intact array to another.

After the first loop I would attempt to print out the contents of both dataint and dataint2 to show they are the same. Then I would do the sort on either dataint or dataint2 and output the sorted data and the unsorted data to show they are different. Then if I wanted to copy the data in the unsorted array int the sorted array or visa versa, I would use the second for loop and reprint both arrays to show the copy process was completed correctly.
• 02-27-2003
Trauts
Thanks elad, I've already run many tests through this, and it works now though. The whole reason that I had to replace dataint with dataint2 was that when you pass the array to these sorts, it changes the actual data that was passed to it, so when it leaves the function it is already changed... the functions are voids.