Hi all, hoping someone can help me with a bit of code problem.
I have this simple C++ program that does some processing on a text file with a bunch of numbers in it. I'm reading the numbers into global structures and then doing some math, then overriting those global structures with the next set of numbers.
The problem is, after 18,000 or so cycles (I need it to run 80,000 cycles or so) I get a stack overflow error. My guess is that I have a memory leak somewhere, but I haven't got a clue how to fix it. I thought C++ managed memory for you! Anyway, I am hoping someone can help. I have attached my code and a zip of the .cpp and two sample intake files.
Thanks in advance!
Sample file at YouSendIt (4.11 MB)
http://www.yousendit.com/transfer.php?action=download&ufid=21BBFE7D5AA0DDC1
//---------------------------------------------------------------------------
#pragma hdrstop
#include <stdlib.h>
#include <iostream.h> // I/O
#include <fstream.h> // file I/O
#include <iomanip.h> // format manipulation
//---------------------------------------------------------------------------
//Global Variables
struct statLine{
int id;
int primary_type;
int secondary_type;
double period;
double epochnumber;
int savedextremenumber;
}statLine;
struct minmaxLine{
int id;
double time;
double value;
int type;
double integral;
}previous, current, reading;
ifstream minmaxFS, statsFS;
ofstream errorFSO, deltaTFSO;
FILE *deltaTFP;
//---------------------------------------------------------------------------
void shiftLine();
void getDeltaT();
void getLines();
void printDeltaT();
void initiate();
//---------------------------------------------------------------------------
//overwrites previous with current.
void shiftLine()
{
previous.id = current.id;
previous.time = current.time;
previous.value = current.value;
previous.type = current.type;
previous.integral = current.integral;
current.id = reading.id;
current.time = reading.time;
current.value = reading.value;
current.type = reading.type;
current.integral = reading.integral;
}
//goes to last min-max interval in neuron and gets the deltaT
void getDeltaT(){
double time;
minmaxFS >> time;
while(time < statLine.id && !minmaxFS.eof()) //do this until the last one)
{
shiftLine();
reading.time = time;
minmaxFS >> reading.value >> reading.type >> reading.integral;
minmaxFS >> time;
}
//It is the last one now
if(statLine.id == 12245421){
cout << "Hi!" << endl;
}
if(minmaxFS.eof())
{
cout << "Program Finished!" << endl;
}
else
{
if (reading.type == 0)//It's a minimum
shiftLine();
printDeltaT();
reading.id = time;
getLines();
}
}
//Starts this program. Gets first code number to process.
void initiate()
{
minmaxFS >> reading.id;
getLines();
}
//Gets the next neuron to process
void getLines()
{
statsFS >> statLine.id >> statLine.primary_type >> statLine.secondary_type >> statLine.period >> statLine.epochnumber >> statLine.savedextremenumber;
while(statLine.id != reading.id)
{
double temp;
char temp2[20];
minmaxFS >> temp2;
temp = atoi(temp2);
if (temp == statLine.id)
reading.id = statLine.id;
if (minmaxFS.eof())
{
errorFSO << "Error matching :" <<statLine.id<<endl;
exit(EXIT_FAILURE);
}
}
minmaxFS >> reading.time >> reading.value >> reading.type >> reading.integral;
getDeltaT();
}
//Prints the id of the neuron, spike frequency, and time differenece between spikes
void printDeltaT()
{
double deltaT, frequency;
int eyedee;
deltaT = current.time - previous.time;
if(statLine.period == 0)
frequency = 0;
else
frequency = (1/statLine.period);
deltaTFSO << statLine.id << " " << frequency << " " << deltaT << endl;
}
//---------------------------------------------------------------------------
#pragma argsused
int main(void)
{
minmaxFS.open("minmax.dat");
statsFS.open("stats.dat");
deltaTFSO.open("0Thalam_deltaT.dat");
errorFSO.open("0Thalam_Terror.txt");
if(minmaxFS.bad()|| statsFS.bad() || deltaTFSO.bad() || errorFSO.bad())
cout << "Error at file access!" << endl;
else
initiate();
minmaxFS.close();
statsFS.close();
deltaTFSO.close();
errorFSO.close();
return 0;
}