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


LinkBack URL
About LinkBacks




Reply With Quote



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum