Thread: Help me with dynamically allocating!

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    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

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    i didn't read your whole post, but about the first part just do

    int size = 0;
    cin >> size;

    int *array;
    array = new int[size];

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    delete dataint2 first?, or better set it all to NULL pointers before deleting it

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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"

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I fixed it by doing this:

    Code:
    for (i=0; i<size; i++) {dataint[i] = dataint2[i];}
    And I had already dynamically allocated it :-P

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. dynamically allocating memory
    By rs07 in forum C Programming
    Replies: 6
    Last Post: 09-14-2008, 03:26 AM
  3. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  4. Help Dynamically allocating a struct
    By Bnchs in forum C Programming
    Replies: 5
    Last Post: 12-07-2007, 07:00 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM