+ Reply to Thread
Results 1 to 4 of 4

Thread: Dynamic Arrays

  1. #1
    Newbie Fedex is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    4

    Dynamic Arrays

    Good afternoon, all!

    I've been having some problems with a section of code, and I was hoping someone could give me some insight into how to get it working.

    The program is designed to take data from an input text file (in the format "a SPACE b SPACE c" where a is an integer and b and c are doubles) and perform a mathematical operation on the data (a Fourier Transform, if anyone's interesting). The part of the code that isn't working is the part that loads the data from the input file.

    The data is loaded into arrays, such that the array Ref contains the middle column of the input file, and Imf the right column.

    Obviously, the program has no way of knowing the amount of data it'll have to work on, so I decided to start of with the arrays Ref and Imf of size 1000, and dynamically increase them.

    The program increases the size the first time (i.e, if there are 1500 data values, then the program works fine) but if it needs to increase the size more than once, the program crashes (compiles perfectly though).

    The crash seems to come on the "delete []" lines.

    Can anyone give me any hints on why it might not be working?

    Regards,

    Steve

    Code:
     
    
    noe = 1000; // noe = Number Of Elements (in the array)
    inc = 1000; //Amount to increase "noe" by each time
    
    double * Ref = new double[noe];  
    double * Imf = new double[noe];  
    //Declare dynamic arrays Ref and Imf of size 1000 initally
    
    // OTHER CODE
    
    double * tempa;
    double * tempb;
    //Two temporary arrays
    
    // OTHER CODE
    
    while (fscanf(input, " %d %lf %lf", &a, &b, &c)!= EOF)
      {
            Ref[a] = b;
            Imf[a] = c;
            printf("Successfully read data value %d!\n",a);
          
            if(a >= noe) 
            {
               noe = noe + inc;            // change the previous size           
               tempa = new double[noe]; // create new bigger array.
               
               for (int i=0; i<(noe-inc); i++) 
               {
                    tempa[i] = Ref[i];       // copy values to new array.
               }       
               
               delete [] Ref;              // free old array memory.  
               Ref = tempa;                 // now a points to new array.
             
    
               tempb = new double[noe]; // create new bigger array.
               
               for (int i=0; i<(noe-inc); i++) 
               {
                    tempb[i] = Imf[i];       // copy values to new array.
               } 
               
               delete [] Imf;              // free old array memory.
               Imf = tempb;                 // now a points to new array.
    
                          
               tempa = NULL;
               tempb = NULL;
               //Clear the pointers 
               printf("Array is now size %d\n",noe);
               system("PAUSE");
    
            }
    
    //OTHER CODE

  2. #2
    Guru G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough G_Morgan is a jewel in the rough
    Join Date
    Oct 2007
    Age
    25
    Posts
    537
    Why not use a Vector or some other STL class that can grow or shrink?

  3. #3
    Newbie Fedex is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    4
    Hmm. That's not a bad idea - it simply never occurred to me. I'll try doing it that way. Thanks.

  4. #4
    Newbie Fedex is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    4
    It seems to be working quite nicely now. Thanks for the suggestion.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Help regarding the sorting of arrays with flags!
    By Yuriy M in forum C and C++
    Replies: 3
    Last Post: 10-12-2007, 10:30 PM
  2. Python arrays...
    By Sir_Rimo in forum Python
    Replies: 3
    Last Post: 06-20-2007, 08:54 AM
  3. Arrays
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 08:30 PM
  4. Arrays
    By Sionofdarkness in forum C and C++
    Replies: 5
    Last Post: 07-26-2006, 05:35 PM
  5. Arrays in NET 2.0 or CLI Managed C++
    By Void in forum Managed C++
    Replies: 1
    Last Post: 07-18-2006, 07:57 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts