Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-2007, 12:08 PM
Fedex Fedex is offline
Newbie
 
Join Date: Dec 2007
Posts: 4
Rep Power: 0
Fedex is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-02-2007, 04:55 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

Why not use a Vector or some other STL class that can grow or shrink?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-02-2007, 05:21 PM
Fedex Fedex is offline
Newbie
 
Join Date: Dec 2007
Posts: 4
Rep Power: 0
Fedex is on a distinguished road
Default

Hmm. That's not a bad idea - it simply never occurred to me. I'll try doing it that way. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-02-2007, 05:45 PM
Fedex Fedex is offline
Newbie
 
Join Date: Dec 2007
Posts: 4
Rep Power: 0
Fedex is on a distinguished road
Default

It seems to be working quite nicely now. Thanks for the suggestion.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 11:02 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads