C++ exceed buffer limit = abort with cuteness

Nov 24, 2014 at 3:43am
Hi guys,

I need another help,
I have this problem with my output file.

Normally, i use iterative methods to compute for things. but then, as iteration goes, the number of elements doubles. So this posts a problem with my buffer size.

How can I tell the program that if the buffer size is exceeded, it should stop its process already?

Thanks for ideas.
Nov 24, 2014 at 4:10am
You can throw an exception or return an error code from the function. Of course the code that calls it will have to deal with the exception or return value.

But maybe there is another way to accomplish the task. Can you describe the problem a little more and/or post some code?
Nov 24, 2014 at 7:03am
Here's part of the code that I am working on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
do //main process iteration
					{
						sum=0.0;
						stepsize = (upperlimit-lowerlimit)/numOfSteps;

						for (i=0; i<=numOfSteps;i++)
						{
							if (i==0)
							{
								subPoint[0]=lowerlimit;
							}
							else
							{
								subPoint[i] = subPoint[i-1] + stepsize;
							}
						}

						for (i=0; i<=numOfSteps; i++)
						{
							fxEvaluate[i] = equation.sniff(expstr,subPoint[i]);
						}

						for (i=0; i<=numOfSteps; i++)
						{
							if (i==0 || i==numOfSteps)
							{
								sum = sum + fxEvaluate[i]/2;
							}
							else
							{
								sum = sum + fxEvaluate[i];
							}
						}
						answerCurrent = sum * stepsize;
						currentError = abs(answerCurrent - answerPrevious);
						cout << endl << endl << "   " << trialNum << "\t   " << stepsize << "\t" << answerCurrent << "\t" << currentError;
						answerPrevious = answerCurrent;
						numOfSteps = numOfSteps*2;
						trialNum++;
					} while(currentError >= errorptolerance);


subPoint only has 200,000 buffer size.
Then the loop goes on and on and on.. :(
Nov 24, 2014 at 9:12am
can you change subPoint to be a std::vector collection? Then it'll only get as big as it's needs to get.
Nov 24, 2014 at 1:23pm
You don't need the arrays at all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
do                              // main process iteration
{
    sum = 0.0;
    double subPoint, fxEvaluate;

    stepsize = (upperlimit - lowerlimit) / numOfSteps;

    for (i = 0; i <= numOfSteps; i++) {
        subPoint = lowerLimit + i*stepsize;
        fxEvaluate = equation.sniff(expstr, subPoint);
        if (i ==0 || i == numOfSteps) {
            sum += fxEvaluate/2;
        } else {
            sum += fxEvaluate;
        }
    }
    answerCurrent = sum * stepsize;
    currentError = abs(answerCurrent - answerPrevious);
    cout << endl << endl << "   " << trialNum << "\t   " << stepsize << "\t" <<
        answerCurrent << "\t" << currentError;
    answerPrevious = answerCurrent;
    numOfSteps = numOfSteps * 2;
    trialNum++;
} while (currentError >= errorptolerance);

Last edited on Nov 24, 2014 at 1:24pm
Nov 24, 2014 at 1:30pm
Oh, I get it. you have another way of computing the trapezoidal rule.

Thanks for the advise dhayden. :)
Topic archived. No new replies allowed.