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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This assumes all children of container_id are divs: function show_item(… May 12, 2026 at 10:30 am
  • Editorial Team
    Editorial Team added an answer In a recent project of mine I did it using… May 12, 2026 at 10:30 am
  • Editorial Team
    Editorial Team added an answer You should have a repository for each aggregate root. I… May 12, 2026 at 10:30 am

Related Questions

I was poking around in XNA and saw that the Vector3 class in it
An object is positioned at A and wants to move to B . I
I'm trying to fix something in some Objective C++ (?!) code. I don't know
I have a range of random numbers. The range is actually determined by the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.