Find the answer to your Linux question:
Results 1 to 6 of 6
I am trying to make a vector class with some functions and operators, and I was wondering if the following would cause a memory leak: Code: class vector { private: ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3

    is this a memory leak?

    I am trying to make a vector class with some functions and operators, and I was wondering if the following would cause a memory leak:

    Code:
    class vector
    {
      private:
    
      int size;
      double *value;
    
      public:
    
      vector(vector&);
      vector(int);
      vector() { value = NULL; };
      ~vector () {delete [] value;};
    
    
      int get_size() { return size; };
      double &operator () (int a) { return (value[a]); };
    
      vector &operator + (vector&);
    };  
    
    vector::vector (int a)
    {
      size = a;
      value = new double[size];
    }
    
    vector::vector (vector& a)
    {
      size = a.get_size();
      value = new double[size];
      for (unsigned i=0; i<size; ++i)
        value[i] = a(i);
    }
    
    vector &vector::operator+ (vector& a)
    {
      vector temp(size);
      for (unsigned i=0; i<size; ++i)
        temp(i) = value[i] + a(i);
      return (*(new vector(temp)));  // does this statement cause a leak???
    }
    This statement will run millions of times so I want to make sure that it's not leaking memory all over the place. Any help on this would be much appreciated.

  2. #2
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33
    #run the program & check the resources being used by the command

    lsof -p process-id

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3

    it does leak

    I ran some simple tests and it is causing a memory leak.

    But how can I change the + operator function to not leak memory?

    I've tried many other varieties but none of them work other than returning the local variable itself (which is not a good way to do it anyways).

  4. #4
    Just Joined!
    Join Date
    Aug 2007
    Posts
    7

    Memory Leak

    you can change the Prottotype of the overloaded operator to:-

    vector operator +(vector &v){
    vector temp;


    return temp;
    }

    for this you also need to overload = operator.

  5. #5
    Just Joined!
    Join Date
    Aug 2007
    Posts
    3
    I tried your suggestion, but it won't compile now.

    This code:

    Code:
    #include <iostream>
    
    class vector
    {
      private:
    
      int size;
      double *value;
    
      public:
    
      vector(vector&);
      vector(int);
      vector() { value = NULL; };
      ~vector () {delete [] value;};
    
      int get_size() { return size; };
      double &operator () (int a) { return (value[a]); };
    
      vector operator +(vector a){
        vector temp(size);
        for (unsigned i=0; i<size; ++i)
          temp(i) = value[i] + a(i);
        return temp;
      }
      
      vector &operator =(vector& a){
        for (unsigned i=0; i<size; ++i)
          value[i] = a(i);
        return *this;
      }
      
    };  
    
    vector::vector (int a)
    {
      size = a;
      value = new double[size];
    }
    
    vector::vector (vector& a)
    {
      size = a.get_size();
      value = new double[size];
      for (unsigned i=0; i<size; ++i)
        value[i] = a(i);
    }
    
    int main(void)
    {
      vector x(100);
      vector y(100);
      for (unsigned i=0; i<100; ++i)
      {
        x(i) = 3.0;
        y(i) = 0.0;
      }
    
      y = x+x;
    
      return 0;
    }
    Gives the error message:
    Code:
    In function 'int main()':
    error: no match for 'operator=' in 'y = vector::operator+(vector)(vector(((vector&)(& x))))'
    note: candidates are: vector& vector::operator=(vector&)

  6. #6
    Just Joined!
    Join Date
    Aug 2007
    Posts
    7
    sorry Its my Bad, Please comment the code :-

    /* vector &operator =(vector& a){
    for (unsigned i=0; i<size; ++i)
    value[i] = a(i);
    return *this;
    }
    */
    there is no need to overload the = Operator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...