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 7058071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:04:08+00:00 2026-05-28T04:04:08+00:00

I was searching about this topic and I found many ways to convert an

  • 0

I was searching about this topic and I found many ways to convert an array[] to an std::vector, like using:

assign(a, a + n)

or, direct in the constructor:

std::vector<unsigned char> v ( a, a + n );

Those solve my problem, but I am wondering if it is possible (and correct) to do:

myvet.resize( 10 );
memcpy( &myvet[0], buffer, 10 );

I am wondering this because I have the following code:

IDiskAccess::ERetRead nsDisks::DiskAccess::Read( std::vector< uint8_t >& bufferRead, int32_t totalToRead )
{
    uint8_t* data = new uint8_t[totalToRead];
    DWORD totalRead;
    ReadFile( mhFile, data, totalToRead, &totalRead, NULL );
    bufferRead.resize( totalRead );
    bufferRead.assign( data, data + totalRead );
    delete[] data;

    return IDiskAccess::READ_OK;
}

And I would like to do:

IDiskAccess::ERetRead nsDisks::DiskAccess::Read( std::vector< uint8_t >& bufferRead, int32_t totalToRead )
{
    bufferRead.resize( totalToRead );
    DWORD totalRead;
    ReadFile( mhFile, &bufferRead[0], totalToRead, &totalRead, NULL );
    bufferRead.resize( totalRead );

    return IDiskAccess::READ_OK;
}

(I have removed the error treatment of the ReadFile function to simplify the post).

It is working, but I am affraid that it is not safe. I believe it is ok, as the memory used by the vector is continuous, but I’ve never seen someone using vectors this way.

Is it correct to use vectors like this? Is there any other better option?

  • 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-05-28T04:04:09+00:00Added an answer on May 28, 2026 at 4:04 am

    Yes it is safe with std::vector C++ standard guarantees that the elements will be stored at contiguous memory locations.

    C++11 Standard:

    23.3.6.1 Class templatevector overview [vector.overview]

    A vector is a sequence container that supports random access iterators. In addition,itsupports(amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that ifv is avector whereT is some type other than bool, then it obeys the identity&v[n] == &v[0] + n for all0 <= n < v.size().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been searching about this topic but i don't seem to find anything concrete
I've been searching a lot but I haven't found anything about this question. I'm
This is some what of a supplementary question to my recent query about searching
I have spent about half a day searching for an answer to this question
If this was previously talked about, I'm sorry, I had a hard time searching
I cant understand what this warning I get on Xcode is about. Searching for
i have one problem controlling checkbox, i'm reading, and searching about the topic but
I was searching here about converting a string like 16:20 to a DateTime type
I'm here to ask a specific topic - I really found few info about
I know there are many posts about this issue however I cannot find one

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.