Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:33:37+00:00 2026-06-09T23:33:37+00:00

I have a class that has (amongst many other things) a pointer to unsigned

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:33:38+00:00Added an answer on June 9, 2026 at 11:33 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that (amongst other things) has a command line interface that
I have a class that has 3 string properties. I want to store these
I have an activity which extends ListActivity. It has many things but amongst them
I have a class that has a password property that I want to store
Dojo has a basic issue, amongst other things... with its dojo.dnd.move class. The issue
I have a class that has an explicit initialization method and some other methods
I have a class that has the variable Magic. This is a 4 char
I have a class that has many settable/gettable attributes. I'd like to use reflection
If I have a class that has many int , float , and enum
I have a Class that has a private variable with a public setter/getter function:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.