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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:01:03+00:00 2026-05-27T01:01:03+00:00

This is pretty farfetched, but is the following code safe (i.e. guaranteed not to

  • 0

This is pretty farfetched, but is the following code “safe” (i.e. guaranteed not to cause segmentation fault):

std::vector<int> vec(1); // Ensures that &vec[0] is valid
vec.reserve(100);
memset(&vec[0], 0x123, sizeof(int)*100); // Safe?

I realize that this is ugly – I’m only interested to know if it’s technically safe, not “pretty”. I guess its only usage could be to ignore values stored beyond a given index.

Note! How can I get the address of the buffer allocated by vector::reserve()? covers the same topic, but I’m more interested if this is safe and if there are pitfalls doing this.

EDIT: Original code was wrong, replaced original memcpy with memset.

  • 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-27T01:01:03+00:00Added an answer on May 27, 2026 at 1:01 am

    No, it is not safe.

    After a reserve(), the vector is guaranteed not to reallocate the storage until the capacity() is reached.

    However, the standard doesn’t say what the vector implementation can do with the storage between size() and capacity(). Perhaps it can be used for some internal data – who knows? Perhaps the address space is just reserved and not mapped to actual RAM?

    Accessing elements outside of [0..size) is undefined behavior. There could be some hardware check for that.

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

Sidebar

Related Questions

This is pretty trivial, but I noticed on SO that instead of an offset
I have the following problem. I have this pretty class and now I want
I've googled this question repeatedly, but have not found a great answer anywhere. I'm
Ok I know jquery can do this pretty simply, but digging around I'm a
I'm pretty new to scala and I am not able to solve this (pretty)
update: the answer from virtualeyes (below) looks pretty nice: but now a bit code-sanititzing
I came across this pretty cool wizard menu, but I can't figure out how
i should say hi experts :D . Help me with this pretty code :)
I have the following code: function ViewModel() { var self = this; self.deckareax =
This pretty much has me defeated. On XP and earlier versions of Windows you

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.