mathematician1975
mathematician1975

Reputation: 21351

How can I do equivalent of memcpy from a raw array to a std::vector?

I have a class that has (amongst many other things) a pointer to unsigned char that gets deleted and reallocated to store some data from another array. This done with a function

 class MyClass {
  private:
      unsigned char* m_Buffer;
      int m_BufferSize;
  public:
      bool SetBuffer(int iSize, const unsigned char* pArray); 
 };

 bool MyClass::SetBuffer(int iSize, const unsigned char* pArray) {
     bool bOK = false;
     if (pArray != NULL  && iSize > 0) {
         delete [] m_Buffer;
         m_Buffer = new unsigned char[iSize];
         memcpy(m_Buffer,pArray,iSize);
         m_BufferSize = iSize;
         bOK = true;
      }
      return bOK;
  }

I dont like this code at all, and I would really like to replace the pointer with a std::vector<unsigned char>. My question is, how would I perform the memcpy aspect? If I were passing a vector in as argument to my function, I could copy it using iterators, but I have no control over parameter argument type so I am stuck with unsigned char* . Is there a way of using iterators, or sizing the vector to the right size and then accessing its internal array so that I can still copy the data with memcpy ? Or even better something using iterators?? I know I could use a loop and push_back but that seems painfully inefficient to me. Any suggestions will be gratefully received.

Upvotes: 7

Views: 4534

Answers (2)

PaperBirdMaster
PaperBirdMaster

Reputation: 13298

I really advise you to avoid raw pointers this way. I think that is a better idea to manage std::vectors instead of the raw pointers.

class MyClass {
private:
    std::vector<unsigned char> m_Buffer;
    // No size member is needed, it is stored in m_Buffer
public:
    // No size parameter is needed, it is stored in Array
    void SetBuffer(const std::vector<unsigned char> &Array);
};

void MyClass::SetBuffer(const std::vector<unsigned char> &Array) {
    std::copy(Array.begin(), Array.end(), m_Buffer.begin());
}

Assuming that your dessing forces to have a raw managed for MyClass you will take care of this pointer on copy constructors and operator = (or get rid of it instead):

MyClass::MyClass(const MyClass &Class) {
    m_BufferSize = Class.m_BufferSize;
    m_Buffer = new new unsigned char[m_BufferSize];
    memcpy(m_Buffer, Class.m_Buffer, m_BufferSize); 
}

MyClass::operator =(const MyClass &Class) {
    if (m_Buffer) delete [] m_Buffer;
    m_BufferSize = Class.m_BufferSize;
    m_Buffer = new new unsigned char[m_BufferSize];
    memcpy(m_Buffer, Class.m_Buffer, m_BufferSize); 
}

If you don't take care of the MyClass managed pointers in the copy constructor and operator = you will end with two instances of MyClass managing the same memory.

Upvotes: -1

Matthieu M.
Matthieu M.

Reputation: 299989

Actually, iterators are modelled from pointers and therefore pointers within an array are considered to implement the RandomAccessIterator concept.

Therefore:

m_buffer.assign(pArray, pArray + Size);

Upvotes: 20

Related Questions