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)

#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;

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)
                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;
                cout << "Program Finished!" << endl;
                 if (reading.type == 0)//It's a minimum

                reading.id = time;

//Starts this program.  Gets first code number to process.
void initiate()
        minmaxFS >> reading.id;

//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;

        minmaxFS >> reading.time >> reading.value >> reading.type >> reading.integral;



//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;
                frequency = (1/statLine.period);
        deltaTFSO << statLine.id << " " << frequency << " " << deltaT << endl;


#pragma argsused
int main(void)
        if(minmaxFS.bad()|| statsFS.bad() || deltaTFSO.bad() || errorFSO.bad())
                cout << "Error at file access!" << endl;

        return 0;

Recommended Answers

All 2 Replies

Try to rewrite it without the recursion of getDeltaT calling getLines and vice-versa.

That did the trick! I gotta watch out for weird recursion in the future :)

Thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.