c++copy-constructormemcpyassignment-operator

using memcpy for copy constructor and assignment operator for 2d array


I have 2 classes. Pointer to an array of fixed width and Pointer to a Pointer for implementing 2d array of floats.
I wanted to understand, if and how can I use memcpy for my Copy constructor and Assignment operator overload.

class UsingPtrToArr {
  public:
    UsingPtrToArr() {
      m_rows = 1;
      f = new float[m_rows][8];
      for (int i = 0; i < 8; ++i)
      {
        f[0][i] = i+1;
      }
    }
    UsingPtrToArr(const UsingPtrToArr& iv) {
      m_rows = iv.m_rows;
      f = new float[m_rows][8];
      for(int r = 0; r < m_rows; ++r) {
        for (int i = 0; i < 8; ++i)
          f[r][i] = iv.f[r][i];
      }
      sum = iv.sum;
    }
    UsingPtrToArr& operator= (const UsingPtrToArr& iv) {
      m_rows = iv.m_rows;
      f = new float[m_rows][8];
      for(int r = 0; r < m_rows; ++r) {
        for (int i = 0; i < 8; ++i)
          f[r][i] = iv.f[r][i];
      }
      sum = iv.sum;
      return *this;
    }
    void sumArray() {
      for(int r = 0; r < m_rows; ++r) {
        for (int i = 0; i < 8; ++i)
          sum += f[r][i];
      }
    }

    ~UsingPtrToArr() {
      delete[] f;
    }
  private:
    float (*f)[8];
    double sum = 0;
    int m_rows;
};

another class

class UsingPtrToPtr {
  public:
    UsingPtrToPtr() {
      int row = 1;
      int col = 8;
      m_rows = row;
      m_cols = col;
      f = new float*[row];
      for(int i = 0; i < row; ++i) {
        f[i] = new float[col];
      }
      for (int i = 1; i < row; ++i)
      {
        for (int j = 0; j < col; ++j) {
          f[i][j] = j+1;
        }
      }
    }
    UsingPtrToPtr(const UsingPtrToPtr& iv) {
      sum = iv.sum;
      m_rows = iv.m_rows;
      m_cols = iv.m_cols;
      f = new float*[m_rows];
      for(int i = 0; i < m_rows; ++i) {
        f[i] = new float[m_cols];
      }
      for (int i = 1; i < m_rows; ++i)
      {
        for (int j = 0; j < m_cols; ++j) {
          f[i][j] = iv.f[i][j];
        }
      }
      sum = iv.sum;
    }
    UsingPtrToPtr& operator= (const UsingPtrToPtr& iv) {
      m_rows = iv.m_rows;
      m_cols = iv.m_cols;
      f = new float*[m_rows];
      for(int i = 0; i < m_rows; ++i) {
        f[i] = new float[m_cols];
      }
      for (int i = 1; i < m_rows; ++i)
      {
        std::memcpy(f[i], iv.f[i], sizeof(float) * m_cols);
        for (int j = 0; j < m_cols; ++j) {
          f[i][j] = iv.f[i][j];
        }
      }
      sum = iv.sum;
      return *this;
    }
    void sumArray() {
      for (int i = 1; i < m_rows; ++i)
      {
        for (int j = 0; j < m_cols; ++j) {
          sum += f[i][j];
        }
      }
    }
    ~UsingPtrToPtr() {
      for (int i = 1; i < m_rows; ++i)
      {
        delete[] f[i];
      }
      delete[] f;
    }
  private:
    float **f;
    int m_rows;
    int m_cols;
    double sum = 0;
};

Disclaimer: This for my understanding purpose. I know, I can use std::vector in C++.


Solution

  • The below code seems to work

    UsingPtrToArr(const UsingPtrToArr& iv) {
          m_rows = iv.m_rows;
          f = new float[m_rows][8];
          std::memcpy(f, iv.f, sizeof(float)*m_rows*8);
          sum = iv.sum;
    }
    

    and for UsingPtrToPtr, I used the below code

        UsingPtrToPtr(const UsingPtrToPtr& iv) {
              sum = iv.sum;
              m_rows = iv.m_rows;
              m_cols = iv.m_cols;
              f = new float*[m_rows];
              for(int i = 0; i < m_rows; ++i) {
                f[i] = new float[m_cols];
              }
              for (int i = 0; i < m_rows; ++i)
              {
                std::memcpy(f[i], iv.f[i], sizeof(float)*m_cols);
              }
              sum = iv.sum;
    }