GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Feb-2010, 08:00
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Problem dealocating Memory!


Hello, I am making this program for school, and I am stuck ... I think the program is not assigning and dealocating the memory correctly because the program sometimes runs and sometimes crashes... plz take a look and see if you see anything wrong with the code.. thanks!

(I am getting the file's path through user input...)

CPP / C++ / C Code:

void addToStruct ()
     {
                 
                 int tempSize = rainFallDataFile.size();
                 string rainFallAmount = "";
                
                 char* charRainfallData;
                 charRainfallData = new char [tempSize];
                 strcpy (charRainfallData, rainFallDataFile.c_str());
                 ifstream inputFile;                 
                 inputFile.open(charRainfallData, ios::in); 
                 
                 if(inputFile.fail())
                 {
                                     cout << "Error Opening the File." << endl;
                 }
                 
                 string line = "";
                 char* sRainfall;
                 double monthRainfall = 0.0;
                 getline(inputFile, line);

                 int counter = 0;
                 char skipHeader[20];
                 while (line[counter] != ',')
                 {
                       skipHeader[counter] += line[counter];
                       counter++;                       
                 }  
                 counter++;
                 
                 for(int insideCounter = 0; insideCounter <= 11; insideCounter++)
                 {
                 while (line[counter] != ',')    
                 {
                       rainFallAmount += line[counter];
                       counter++;
                 } 
                 
                 sRainfall = new char [rainFallAmount.size()+1];
                 strcpy (sRainfall, rainFallAmount.c_str());
                 monthRainfall = atof(sRainfall);
                 miamiWeather[insideCounter].monthlyRainfall = monthRainfall;
                 rainFallAmount = "";                 
                 counter++;                 
                 }
                   
                 inputFile.close();
                 delete [] charRainfallData;
                 delete [] sRainfall;

     }
     
     void addToStructTwo ()
     {
                 int tempSizeTwo;
                 tempSizeTwo = averageHighData.size();
                 string avgHighData = "";
                
                 char* charAvgHighData;
                 charAvgHighData = new char[tempSizeTwo];
                 strcpy (charAvgHighData, averageHighData.c_str());
                 
                 ifstream inputTwo;                 
                 inputTwo.open(charAvgHighData, ios::in); 
                 
                 if(inputTwo.fail())
                 {
                    cout << "Error Opening the File." << endl;
                 }
               
                 
                 string lineTwo = "";
                 char* sAvgHigh;
                 double avgHigh = 0.0;
                 getline(inputTwo, lineTwo);

                 int counterTwo = 0;
                 char skipHeaderTwo[40];
                 while (lineTwo[counterTwo] != ',')
                 {
                       skipHeaderTwo[counterTwo] += lineTwo[counterTwo];
                       counterTwo++;                       
                 }  
                 counterTwo++;
                 
                 for(int insideCounterTwo = 0; insideCounterTwo <= 11; insideCounterTwo++)
                 {
                 while (lineTwo[counterTwo] != ',')    
                 {
                       avgHighData += lineTwo[counterTwo];
                       counterTwo++;
                 } 
                 
                 sAvgHigh = new char [avgHighData.size()+1];
                 strcpy (sAvgHigh, avgHighData.c_str());
                 avgHigh = atof(sAvgHigh);
                 miamiWeather[insideCounterTwo].aveHighTemp = avgHigh;
                 avgHighData = "";                 
                 counterTwo++;                 
                 }
                 
                 for(int insideCounterTw = 0; insideCounterTw <= 11; insideCounterTw++)
                 {
                  cout << miamiWeather[insideCounterTw].aveHighTemp;
                 }
                 
                 inputTwo.close();
                 delete [] charAvgHighData;
                 delete [] sAvgHigh;
     } 


  #2  
Old 08-Feb-2010, 10:05
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 815
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Problem dealocating Memory!


The first thing that just jumped out at me is this:

CPP / C++ / C Code:
 int tempSize = rainFallDataFile.size();
//...
charRainfallData = new char [tempSize];
strcpy (charRainfallData, rainFallDataFile.c_str());

You need to allocate space for the terminator at the end of the string so you would need tempSize+1 chars to store a string of tempSize characters.

NOTE: I did not look at anything else in your code...this is just something I saw at the very beginning. It may or may not solve your problem.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Complete Guide to SD Memory Cards haulkook Computer Hardware Forum 0 01-Jul-2008 23:27
A Few Tips on Buying Good Memory Cards and other Hi-Tech Electronic Accessories haulkook Computer Hardware Forum 0 02-Jun-2008 03:53
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 08:09
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:04.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.