memory leak problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bingo

    #1

    memory leak problem

    Hi, All:
    I'm really new to C++, so please forgive If I asked stupid
    questions:

    I was trying to use the following Vector class to do some
    calculations:

    =============== ==============
    class Vector {
    public:
    double x,y,z;

    inline Vector(void) : x(-99999), y(-99999), z(-99999) { ; }
    // inline Vector(void) { ; }

    inline Vector( double newx, double newy, double newz)
    : x(newx), y(newy), z(newz) { ; }

    inline Vector( double newv ) // allow Vector v = 0; etc.
    : x(newv), y(newv), z(newv) { ; }

    inline Vector(const FloatVector &v) : x(v.x), y(v.y), z(v.z)
    { ; }

    inline double operator[](int i) {
    return i==0 ? x
    :i==1 ? y
    :i==2 ? z
    : x;

    }

    // v1 = v2
    inline Vector& operator=(const Vector &v2) {
    x = v2.x;
    y = v2.y;
    z = v2.z;
    return *this;
    }

    // v1 = const;
    inline Vector& operator=(const double &v2) {
    x = v2;
    y = v2;
    z = v2;
    return *this;
    }

    // addition of two vectors
    inline friend Vector operator+(const Vector& v1, const Vector&
    v2) {
    return Vector( v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
    }

    // negation
    inline friend Vector operator-(const Vector &v1) {
    return Vector( -v1.x, -v1.y, -v1.z);
    }

    // subtraction
    inline friend Vector operator-(const Vector &v1, const Vector
    &v2) {
    return Vector( v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
    }

    // inner ("dot") product
    inline friend double operator*(const Vector &v1, const Vector
    &v2) {
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    }
    // scalar product
    inline friend Vector operator*(const double &f, const Vector &v1)
    {
    return Vector(f*v1.x, f*v1.y, f*v1.z);
    }
    // scalar product
    inline friend Vector operator*(const Vector &v1, const double &f)
    {
    return Vector(f*v1.x, f*v1.y, f*v1.z);
    }
    // division by a scalar
    inline friend Vector operator/(const Vector &v1, const double &f)
    {
    return Vector(v1.x/f, v1.y/f, v1.z/f);
    }

    };
    =============== =============== =========

    and my main program looks like this:

    int main () {

    Vector a, b, c, d;
    a = b - c + d;
    a = b * 0.5;
    ......
    }

    Now my question is : for the operation - , + and scalar product, will
    it create a new Vector for return, and not be released in my
    calculation, which finally leads to memory leak? If so, how can I
    solve the problem?

    Thanks a lot.
    -Bin

  • Victor Bazarov

    #2
    Re: memory leak problem

    bingo wrote:
    Hi, All:
    I'm really new to C++, so please forgive If I asked stupid
    questions:
    >
    I was trying to use the following Vector class to do some
    calculations:
    >
    =============== ==============
    class Vector {
    public:
    double x,y,z;
    >
    inline Vector(void) : x(-99999), y(-99999), z(-99999) { ; }
    // inline Vector(void) { ; }
    >
    inline Vector( double newx, double newy, double newz)
    : x(newx), y(newy), z(newz) { ; }
    >
    inline Vector( double newv ) // allow Vector v = 0; etc.
    : x(newv), y(newv), z(newv) { ; }
    >
    inline Vector(const FloatVector &v) : x(v.x), y(v.y), z(v.z)
    { ; }
    >
    inline double operator[](int i) {
    return i==0 ? x
    :i==1 ? y
    :i==2 ? z
    : x;
    >
    }
    >
    // v1 = v2
    inline Vector& operator=(const Vector &v2) {
    x = v2.x;
    y = v2.y;
    z = v2.z;
    return *this;
    }
    >
    // v1 = const;
    inline Vector& operator=(const double &v2) {
    x = v2;
    y = v2;
    z = v2;
    return *this;
    }
    >
    // addition of two vectors
    inline friend Vector operator+(const Vector& v1, const Vector&
    v2) {
    return Vector( v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
    }
    >
    // negation
    inline friend Vector operator-(const Vector &v1) {
    return Vector( -v1.x, -v1.y, -v1.z);
    }
    >
    // subtraction
    inline friend Vector operator-(const Vector &v1, const Vector
    &v2) {
    return Vector( v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
    }
    >
    // inner ("dot") product
    inline friend double operator*(const Vector &v1, const Vector
    &v2) {
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    }
    // scalar product
    inline friend Vector operator*(const double &f, const Vector &v1)
    {
    return Vector(f*v1.x, f*v1.y, f*v1.z);
    }
    // scalar product
    inline friend Vector operator*(const Vector &v1, const double &f)
    {
    return Vector(f*v1.x, f*v1.y, f*v1.z);
    }
    // division by a scalar
    inline friend Vector operator/(const Vector &v1, const double &f)
    {
    return Vector(v1.x/f, v1.y/f, v1.z/f);
    }
    >
    };
    =============== =============== =========
    >
    and my main program looks like this:
    >
    int main () {
    >
    Vector a, b, c, d;
    a = b - c + d;
    a = b * 0.5;
    ......
    }
    >
    Now my question is : for the operation - , + and scalar product, will
    it create a new Vector for return, and not be released in my
    calculation, which finally leads to memory leak? If so, how can I
    solve the problem?
    No, there is no memory leak. The temporary object is created by the
    language and disposed of at the end of its lifetime without your
    explicit instruction. Don't worry about it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    Working...