Closed Thread
Results 1 to 4 of 4

Thread: Dynamic Arrays

  1. #1
    Fedex is offline Newbie
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    Why not use a Vector or some other STL class that can grow or shrink?

  4. #3
    Fedex is offline Newbie
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0
    Hmm. That's not a bad idea - it simply never occurred to me. I'll try doing it that way. Thanks.

  5. #4
    Fedex is offline Newbie
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0
    It seems to be working quite nicely now. Thanks for the suggestion.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. Dynamic CSS
    By bbqroast in forum JavaScript and CSS
    Replies: 1
    Last Post: 10-31-2010, 05:26 AM
  3. Dynamic Arrays: Using malloc() and realloc()
    By Guest in forum C Tutorials
    Replies: 33
    Last Post: 10-25-2010, 08:39 PM
  4. Need Help on arrays and linking to arrays
    By Trendnet18 in forum Java Help
    Replies: 15
    Last Post: 01-19-2010, 11:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

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