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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:38:54+00:00 2026-05-11T05:38:54+00:00

From what i know a vector is guaranteed to be continuous and i can

  • 0

From what i know a vector is guaranteed to be continuous and i can write a chunk of memory to it and do send of fwrite with it. All i need to do is make sure i call .resize() to force it to be the min length i need then i can use it as a normal char array? would this code be correct

v.resize(numOfElements); v.clear(); //so i wont get numOfElements + len when i push back vector<char>v2; v2.resize(numOfElements*SizeOfType); while(...) { ... v.push_bacK(x);  } compress(&v2[0], len, &v[0], len); fwrite(&v2[0], ....) 

noting that i never push back or pop v2 i only resize it once and used it as a char array. Would this be safe? and if i also dumped v that would also be safe(i do push back and clear, i may dump it for testing)

  • 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. 2026-05-11T05:38:55+00:00Added an answer on May 11, 2026 at 5:38 am
    v.resize(numOfElements); v.clear(); //so i wont get numOfElements + len when i push back 

    Well, that above code snippet is in effect allocating and creating elements, just to destroy them again. It’s in effect the same as:

    v.reserve(numOfElements); 

    Just that this code is way faster. So, v.size() == 0 in both cases and v.capacity() might be the same as numOfElements in both cases too (although this is not guaranteed). In the second case, however, the capacity is at least numOfElements, which means the internal buffer will not be reallocated until you have push_back’ed that many elements to your vector. Note that in both cases it is invalid if you try accessing any elements – because there are zero elements actually contained.

    Apart from that, i haven’t figured a problem in your code. It’s safe and i would encourage it so use it instead of a raw new or malloc because of the added safeties it provides. I’m however not sure what you mean by ‘dump v’.

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

Sidebar

Related Questions

I know from Algebra class that with ABC and 123 we can make 216
I know from here that I can pass an ignore option in the jQuery.validate
From what I know, everything about OpenGL changed since 3.1 and now I can't
In C++, in addition to my question Erasing element from Vector , how can
from what I know, CTR mode doesn't use an Initial Vector. It just takes
I know how to get an orientation vector from the accelerometer and the magnetic
How do I remove the ith item from a std::vector ? I know I
I know that you can store a bunch of Parent* objects in a vector.
I need to implement a mechanism where I can initialize a vector of my
I have a decimal variable x0. I want to make a vector from [x0,

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.