HEAP CORRUPTION DETECTED: after Normal block (#195) at 0x00913A10. CRT detected that the application wrote to memory after end of heap buffer

-1

i have the following code on VS for writing a polynomial calculator class. .h file is as below

#pragma once
#include <stdio.h>
#include <iostream>
using namespace std; 

class Poly {
private:
    int coefficient=0;
    int largest_exponent=0;
    int* array = nullptr;
    //string input;
public:
    //constructor
    Poly(int coefficient, int largest_exponent);
    Poly(int coefficient); // assume the exponent or power is 0
    Poly(); // empty constructor 0 coeffecient and 0 exponent
    Poly(const Poly& poly); //copy constructor

    //destructoer
    ~Poly();
    int getCoeff(int num);
    void setCoeff(int coeff, int exp);
    //delete and valgrind

    //Overload

    Poly operator*(const Poly &poly) const;
    Poly operator+(const Poly &poly) const;
    //Poly operator+(const Poly &poly);
    Poly operator-(const Poly &poly) const;

    Poly operator*=(const Poly &poly) const;
    Poly operator+=(const Poly &poly) const;
    Poly operator-=(const Poly &poly) const;

    Poly operator=(const Poly &poly) const;
    Poly operator==(const Poly &poly) const;
    Poly operator!=(const Poly &poly) const;

    friend ostream& operator<<(ostream &os, const Poly &poly);
    friend istream& operator>>(istream &is, Poly &poly);
};

and my .cpp file is as below

#include "pch.h"
#include "Poly.h"


Poly::Poly(int coefficient, int largest_exponent) { // largest component must be +ve value 
    this->largest_exponent = largest_exponent;
    this->coefficient = coefficient;

    this->array = new int[largest_exponent+1];
    for (int i = 0; i <= largest_exponent; i++) {
        this->array[i] = 0;    // Initialize all elements to zero.
    }
    this->array[largest_exponent] = coefficient;
}

Poly::Poly(int coefficient) { // assume the exponent or power is 0
    this->largest_exponent = 0;
    this->coefficient = coefficient;

    this->array = new int[1];
    for (int i = 0; i <= 0; i++) {
        this->array[i] = 0;    // Initialize all elements to zero.
    }
    this->array[0] = coefficient;
}

Poly::Poly() { // empty constructor 0 coeffecient and 0 exponent
    this->coefficient = 0;
    this->largest_exponent = 0;

    //this->array = new int[1];
    this->array = new int[2];
    for (int i = 0; i <= 0; i++) {
        this->array[i] = 0;    // Initialize all elements to zero.
    }   
}

Poly::Poly(const Poly& poly) { //copy constructor
    this->coefficient = poly.coefficient;
    this->largest_exponent = poly.largest_exponent;

    this->array = new int[poly.largest_exponent+1];
    for (int i = 0; i <= poly.largest_exponent; i++) {
        this->array[i] = poly.array[i];    // Initialize all elements to zero.
    }
}

Poly::~Poly() {
    cout << "deleting Polynomial array" << endl;
    delete[] array;
    array = nullptr; //or delete array;
}

int Poly::getCoeff(int num) {
    int coeff = this->array[num];
    if (coeff > -33686019) {
        cout << "coeff of power " << num << " of P is: " << coeff << endl;
        return this->array[num];
    }
    else {
        cout << "this Exponent does not exist in the polynomial" << endl;
        return 0;
    }
}

void Poly::setCoeff(int coeff, int exp) {
    int* newarray = new int[(abs(exp)) + 1];

    for (int i = 0; i <= (abs(exp));i++) {
        newarray[i] = 0;
    }

    for (int i = 0; i <= this->largest_exponent;i++) {
        newarray[i] = this->array[i];
    }
    newarray[(abs(exp))] = coeff;
    this->array = newarray;
    //this->array[abs(exp)] = coeff; 
    if ((abs(exp)) > this->largest_exponent) {
        this->largest_exponent = (abs(exp));
    }
}

ostream& operator<<(ostream &os, const Poly &poly) {
    bool flag = false;

    for (int i = poly.largest_exponent; i >= 0; i--) {
            if (poly.array[i] > 0 && i > 1) {
                os << " +" << poly.array[i] << "x^" << i;
                flag = true;
            }
            if (poly.array[i] < 0 && i > 1) {
                os << " " << poly.array[i] << "x^" << i;
                flag = true;
            }
            if (poly.array[i] > 0 && i == 1) {
                os << " +" << poly.array[i] << "x";
                flag = true;
            }
            if (poly.array[i] < 0 && i == 1) {
                os << " " << poly.array[i] << "x";
                flag = true;
            }
            if (poly.array[i] > 0 && i == 0) {
                os << " +" << poly.array[i];
                flag = true;
            }
            if (poly.array[i] < 0 && i == 0) {
                os << " " << poly.array[i];
                flag = true;
            }
    }
    if (flag == false) {
        os << " 0";
    }
    return os;
}

istream& operator>>(istream &is, Poly &poly) {  
    poly.largest_exponent = 0;  
    int * temparray=new int[0]; 
    int coeff;
    int exp;
    cout << "Enter terms for polynomial A.  Enter a coefficient " << endl;
    if (std::cin >> coeff >> exp) {
        if (coeff != -1 && exp != -1) {
            poly.array[exp] = coeff;
            if (poly.largest_exponent < coeff)
                poly.largest_exponent = coeff;
        }

    }
    return is;
}

Poly Poly::operator*(const Poly &poly) const {
    Poly * temp = new Poly();
    temp->largest_exponent = poly.largest_exponent + this->largest_exponent;

    for (int k = 0; k <= temp->largest_exponent; k++) {
        temp->array[k] = 0;    // Initialize all elements to zero.
    }

    for (int i = 0; i <= this->largest_exponent; i++) {
        for (int j = 0; j <= poly.largest_exponent; j++) {
            temp->array[i + j] += (this->array[i] * poly.array[j]);
        }
    }
    return *temp;
}

Poly Poly::operator+(const Poly &poly) const {
    Poly * temp = new Poly();
    int smallest_exponent;
    if (this->largest_exponent >= poly.largest_exponent) {
        temp->largest_exponent = this->largest_exponent;
        smallest_exponent = poly.largest_exponent;
        for (int i = 0; i <= temp->largest_exponent; i++) {
            temp->array[i] = this->array[i];
        }
    }
    else {
        temp->largest_exponent = poly.largest_exponent;
        smallest_exponent = this->largest_exponent;
        for (int i = 0; i <= temp->largest_exponent; i++) {
            temp->array[i] = poly.array[i];
        }
    }

    for (int i = 0; i <= smallest_exponent; i++) {

        temp->array[i] = poly.array[i] + this->array[i];
    }
    return *temp;
}

Poly Poly::operator-(const Poly &poly) const {
    Poly * temp = new Poly();
    int smallest_exponent;

    if (this->largest_exponent >= poly.largest_exponent) {
        temp->largest_exponent = this->largest_exponent;
        smallest_exponent = poly.largest_exponent;
        for (int i = 0; i <= temp->largest_exponent; i++) {
            temp->array[i] = this->array[i];
        }
    }
    else {
        temp->largest_exponent = poly.largest_exponent;
        smallest_exponent = this->largest_exponent;
        for (int i = 0; i <= temp->largest_exponent; i++) {
            temp->array[i] = poly.array[i];
        }
    }

    for (int i = 0; i <= smallest_exponent; i++) {
        temp->array[i] = this->array[i] - poly.array[i];
    }

    if (this->largest_exponent < poly.largest_exponent) {
        for (int i = smallest_exponent+1; i <= poly.largest_exponent; i++) {
            temp->array[i] = temp->array[i]*-1;
        }
    }
    return *temp;   
}

and this is my driver .cpp

#include "pch.h"
#include <iostream>
#include "Poly.h"

int main()
{
    Poly A(-5,1);
    int x = A.getCoeff(1);
    A.setCoeff(7,3);
    A.setCoeff(6,2);
    Poly B(2);
    B.setCoeff(8,2);
    B.setCoeff(8,0);
    Poly C; 
    Poly D(A);
    cout << A << endl;
    cout << B << endl;
    cout << C << endl;
    cout << D << endl;
    Poly E = B + A;
    cout << E << endl;
    Poly F = A-B;
    cout << F << endl;
    Poly G = B-A;   
    cout << G << endl;
    Poly H = B * A;
    cout << H << endl;
    cout << endl;
    //Poly I;
    //cin >> I;
    //cout << I << endl;
    return 0;


}

Whenever itry to run this code, i get the below error even though when i click continue the results seem to be fine. Anyone has an idea where am i messing up the memory?

Debug Error!

Program:

HEAP CORRUPTION DETECTED: after Normal block (#195) at 0x00A03A10. CRT detected that the application wrote to memory after end of heap buffer.

(Press Retry to debug the application) Critical error detected c0000374 Poly.exe has triggered a breakpoint.

Exception thrown at 0x77DC61E4 (ntdll.dll) in Poly.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77DE2378). Unhandled exception at 0x77DC61E4 (ntdll.dll) in Poly.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77DE2378).

'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\user32.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\gdi32.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\imm32.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\msctf.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\combase.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\ole32.dll'. Cannot find or open the PDB file. 'Poly.exe' (Win32): Loaded 'C:\WINDOWS\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file. Exception thrown at 0x77D21796 (ntdll.dll) in Poly.exe: 0xC0000005: Access violation reading location 0x00000000.

c++
heap
asked on Stack Overflow Jan 14, 2019 by AlexAlexis

1 Answer

1

Your code contains pretty much mistakes.

First, don't mark all operators as const if they are not constant. Assignment operator is never constant.

Second, you allocate your array member with a different size depending on constructor. Nice practice is to perform assert when trying to access array member to validate index is not out of bound.

Next, in your setCoef(...) you copy content of old array into new and if new one is smaller than old, you get your access violation:

...
void Poly::setCoeff(int coeff, int exp) {
    int* newarray = new int[(abs(exp)) + 1];

    for (int i = 0; i <= (abs(exp));i++) {
        newarray[i] = 0;
    }

    for (int i = 0; i <= this->largest_exponent;i++) {
        newarray[i] = this->array[i];  // HERE <<<<<<<<<<<<
    }
    ...

Also you dont delete[] your previous array when you reallocate it it setCoef(...)

answered on Stack Overflow Jan 14, 2019 by grapes

User contributions licensed under CC BY-SA 3.0