Thread: DMA help in class constructor

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    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.

    Thanks in advance,

    Carl

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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];
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    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!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM