DMA help in class constructor

• 10-20-2006
clkaiser
DMA help in class constructor
I am working on a class of polynomials with complex coeficients. One of constructors for this class is

Code:

```poly::poly(unsigned int a, complex *b) {         order = a;         std::cout << "\ntestpoint4";         coef = new complex[a + 1];         std::cout << "\ntestpoint5";                 for (int i = order; i >= 0; i--)         {                 coef[i] = b[i];         } }```
I can succesfully test this constructor using

Code:

```complex * tempcoefs;         tempcoefs = new complex[5];         for (int j = 0; j <=4; j++)         {                 tempcoefs[j] = complex(j,j);         }         poly testpoly(4,tempcoefs);         testpoly.printpoly();         delete[] tempcoefs;```
However, one of the methods in the polynomial class is for synthetic division

Code:

```poly poly::synthdiv(complex &z,complex *newcoef) {         horner(z,newcoef); //        now create a new array to return a new polynomial. //        This loop leaves off the final output         complex * newpoly = new complex[int(order)];         for (int i = 1; i <= order+1; i++)         {                 newpoly[i-1] = newcoef[i];         }         poly temp(int(order - 1),newpoly);         delete[] newpoly;         return temp; }```
horner is another method in the polynomial class and is shown below

Code:

```void poly::horner(complex &z, complex *newcoef) {         newcoef[int(order+1)] = complex(0,0);         for (int i = int(order); i >= 0; i--)         {                 newcoef[i] = coef[i+1]+z * newcoef[i+1];         } }```
When this code is executed, the command window shows "testpoint4" but not "testpoint5" and I reach a breakpoint with the following error message

"Windows has triggered a breakpoint in horner.exe.

This may be due to a corruption of the heap, and indicates a bug in horner.exe or any of the DLLs it has loaded.

The output window may have more diagnostic information"

The immediate window of the debuger shows

"HEAP[horner.exe]: Heap missing last entry in committed range near 356678"
where horner.exe is the main program that is calling the synthdiv method

Interestingly if I modify the constructor to be

Code:

```poly::poly(unsigned int a, complex *b) {         order = a;         std::cout << "\ntestpoint4";         complex *test;         test = new complex[order+1];         std::cout << "\ntestpoint5";         delete[] test;         coef = new complex[a + 1];         for (int i = order; i >= 0; i--)         {                 coef[i] = b[i];         } }```
I get exactly the same behavior where the synthdiv method does not work, but the test code shown at the top of this posting does. Once again, "testpoint4" shows on the command window and the now moved "testpoint5" does not. The error messages are the same.

I have been pulling my hair out for 3 days, any help is greatly appreciated.

Carl
• 10-20-2006
QuestionC
Code:

```complex * newpoly = new complex[int(order)]; for (int i = 1; i <= order +1; i++) {         newpoly[i-1] = newcoef[i]; }```
You're going out of bounds on the array here.

If I might make a little suggestion... the way you write the for loop hides the error.

for loops that go up should have the form
i = 0; i < terminator; ++i
Allways start at 0, allways terminate with <

for loops that go down should have the form
i = top - i; i >= 0; --i
Allways terminate with >= 0

Just a suggestion.
Code:

```complex * newpoly = new complex[order]; // <- Either this array isn't big enough ... for (int i = 0; i < order + 1; ++i) { // <- Or this loop is going 1 step too long         newpoly[i] = newcoef[i + 1]; }```
• 10-23-2006
clkaiser
Thank you so much. I implemented your suggestion for for loops throughout the class and found two other places that I was going to be exceeding an index as well. I have also decided to implement a size method so that I can stop worrying about order vs. order +1 for the polynomials. Once again thank you!!!