1

I am trying to access an array with pointer in my c++ class.

below is my class.

#include <iostream>
using namespace std;

class Poly
{
    friend istream &operator>>(istream &, Poly &);
    friend ostream &operator<<(ostream &, const Poly &);

public:
    Poly();
    Poly(int);
    Poly(int, int);
    Poly(const Poly &);
    ~Poly();

    int getCoeff(int) const;
    int getMaxPow() const;
    void setCoeff(int, int);
    void setMaxPow(int);

    Poly operator+(const Poly &) const;
    Poly operator+=(const Poly &);

    Poly operator-(const Poly &) const;
    Poly operator-=(const Poly &);

    Poly operator*(const Poly &) const;
    Poly operator*=(const Poly &);

    Poly operator=(const Poly &);

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

private:
    int* coeffPtr;
    int maxPow;
};

below are my constructors

#include "poly.h"
#include <iostream>
using namespace std;

Poly::Poly() {
    maxPow = 0;
    int eq[1];
    coeffPtr = &eq[0];
    *coeffPtr = 0;
}

Poly::Poly(int coeff) {
    maxPow = 0;
    int eq[1];
    coeffPtr = &eq[0];
    *coeffPtr = coeff;
}

Poly::Poly(int coeff, int maxPower) {
    maxPow = maxPower;
    int eq[maxPower+1];
    coeffPtr = &eq[0];

    for(int i = 0; i < maxPower; i++)
    {
        *(coeffPtr+i) = 0;
    }

    *(coeffPtr+maxPower) = coeff;
}

Poly::Poly(const Poly &other) {
    int eq[other.maxPow];
    coeffPtr = &eq[0];
    for(int i  = 0; i < other.maxPow; i++)
    {
        *(coeffPtr+i) = other.getCoeff(i);
    }
}

int Poly::getCoeff(int pow) const{
    return *(coeffPtr+pow);
}

In my main method, the initial call to getCoeff(number) will return correct element in the array, but it seems that everything gets changed after that initial access.

e.g.,
Poly A(5,7);
A.getCoeff(7); //returns 5
A.getCoeff(7); //returns random memory

What am I doing it wrong?

Thanks you!

epseattle
  • 23
  • 5

1 Answers1

4

You need to allocate memory on the heap using coeffPtr = new int[...] instead of making coeffPtr point to a local variable such as the local variable int eq[...] in your constructors.

Memory for a local variable is allocated on the stack which may/will get overwritten as soon as the local variable goes out of scope. In your case, as soon as program control leaves your constructors, coeffPtr becomes a dangling pointer pointing to memory whose contents may change at any time. Writing to this memory is even worse and will lead to data corruption in other places of your code, or random crashes.

If you allocate the memory on the heap, you will then also have to free this memory using delete[] coeffPtr in your destructors, and deal with the memory in copy constructor and copy assignment...

(Using an std::vector<int> instead of an int[] is probably a better idea, since it frees you from the memory management.)

WhiteViking
  • 3,146
  • 1
  • 19
  • 22
  • 1
    A quick pedantic note (and upvote already granted). On the stack is almost certainly correct for the OP's case, but use of a stack for temporary variables is not mandated by C++. – user4581301 Oct 10 '15 at 17:17
  • 2
    Another side note: std::vector also makes the class [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) compliant with no extra effort. – user4581301 Oct 10 '15 at 17:23