java programmer requests help using ofstream

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 5
Reputation: santa_barbarian is an unknown quantity at this point 
Solved Threads: 0
santa_barbarian santa_barbarian is offline Offline
Newbie Poster

java programmer requests help using ofstream

 
0
  #1
Dec 2nd, 2004
I am a java programmer trying to understand a c++ oddity that I ran into. My c++ program writes a few MB of data using ofstream. Just before the program ends I close the ofstream. However when the program terminates, the amount of free memory on my linux box has decreased by the size of the data that was written. The memory remains lost until the file is deleted. Here is a sample of how I use ofstream:


In my constructor, I have:
ofstream tfile("../../tmp/outputfile.txt", std::ios::app);

a number of prints using tfile occur that are similar to the following line:

tfile << track->GetTrackID() << '\t' << track->GetParentID() << '\t' << pName << '\t';

In the destructor, I have:

tfile.close();

Any ideas would be appreciated![I]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,942
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: java programmer requests help using ofstream

 
0
  #2
Dec 2nd, 2004
It looks to me that you are reading data into a buffer that does not get deleted at the end. Check your code for the ever so elusive buffer.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: santa_barbarian is an unknown quantity at this point 
Solved Threads: 0
santa_barbarian santa_barbarian is offline Offline
Newbie Poster

Re: java programmer requests help using ofstream

 
0
  #3
Dec 2nd, 2004
Thanks for the quick answer! How does one typically delete/find the buffer? tfile->delete was my first guess, but it causes segmantation faults since the ofstream constructor was not called...

The problematic snippet of code writes the interesting data from my monte carlo sim.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: java programmer requests help using ofstream

 
0
  #4
Dec 2nd, 2004
Originally Posted by santa_barbarian
Thanks for the quick answer! How does one typically delete/find the buffer? tfile->delete was my first guess, but it causes segmantation faults since the ofstream constructor was not called...

The problematic snippet of code writes the interesting data from my monte carlo sim.
Can you post that code snippet? It might help if our members had that to look at.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,942
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 911
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: java programmer requests help using ofstream

 
0
  #5
Dec 2nd, 2004
I am not a Java person, but in C++ you best look at something like "new" and the potentially missing "delete". Here is a typical code segment (commented to the hilt): [php]
// Allocate memory
buffer = new char[length];

// Read data as a block
is.read(buffer, length);

// Close file
is.close();

// Print data
cout << buffer << endl;

// Free memory
delete []buffer;
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: santa_barbarian is an unknown quantity at this point 
Solved Threads: 0
santa_barbarian santa_barbarian is offline Offline
Newbie Poster

Re: java programmer requests help using ofstream

 
0
  #6
Dec 2nd, 2004
I have a RunAction class which has the following global line:

std::ofstream tfile("../../tmp/outputfile.txt", std::ios::app);

RunAction::~RunAction() {
tfile.close();
}


A second class SD has the following global line:

extern ofstream tfile;

SD:rocessHits(G4Step* step,G4TouchableHistory* ROhist) {
G4Track* track;
if(step != NULL)
track = step->GetTrack();
else
track = NULL;

G4double edep = 0;
if(step != NULL)
edep = step->GetTotalEnergyDeposit();

bool write = false;
G4int parentID = track->GetParentID();
G4String pName = (track->GetDefinition())->GetParticleName();
G4double kEnergy = track->GetKineticEnergy();
G4double dEnergy = step->GetTotalEnergyDeposit();
G4String volName = track->GetVolume()->GetName();

if((track != NULL) && (step != NULL) && goodEvent) {
// Output to text file.
if((track->GetDefinition()) != NULL)
if((track->GetVolume()) != NULL)
if((step->GetPostStepPoint()) != NULL)
if((step->GetPostStepPoint()->GetProcessDefinedStep()) != NULL)
tfile << track->GetTrackID() << '\t'
<< track->GetParentID() << '\t'
<< pName << '\t'
<< track->GetCurrentStepNumber() << '\t'
<< kEnergy << '\t'
<< dEnergy << '\t'
<< step->GetStepLength() << '\t'
<< track->GetTrackLength() << '\t'
<< track->GetVolume()->GetName() << '\t'
<< step->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() << '\t'
<< track->GetPosition().x() << '\t'
<< track->GetPosition().y() << '\t'
<< track->GetPosition().z() << '\t'
<< track->GetMomentumDirection().x() << '\t'
<< track->GetMomentumDirection().y() << '\t'
<< track->GetMomentumDirection().z() << '\t'
<< track->GetGlobalTime() << '\t'
<< step->GetPreStepPoint()->GetKineticEnergy() << std::endl;
}
}


A couple of other classes use tfile in the same way as the SD class.[INDENT]
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: santa_barbarian is an unknown quantity at this point 
Solved Threads: 0
santa_barbarian santa_barbarian is offline Offline
Newbie Poster

Re: java programmer requests help using ofstream

 
0
  #7
Dec 2nd, 2004
My working hypothysis has changed. I now believe that c++ is NOT memory leaking but instead linux is storing the file in cached memory even after the program has terminated. Deleting the file then forces linux to clear it from cached memory...Thanks for your help!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC