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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:10:46+00:00 2026-05-28T22:10:46+00:00

I found something surprising with std::vector that I thought I’d ask about here to

  • 0

I found something surprising with std::vector that I thought I’d ask about here to hopefully get some interesting answers.

The code below simply copies a string into a char vector and prints the contents of the vector in two ways.

#include <vector>
#include <string>
#include <iostream>

int main()
{
    std::string s("some string");
    std::vector<char> v;
    v.reserve(s.size()+1);

    // copy using index operator
    for (std::size_t i=0; i<=s.size(); ++i)
        v[i] = s[i];

    std::cout << "&v[0]:     " << &v[0] << "\n";
    std::cout << "begin/end: " << std::string(v.begin(), v.end()) << "\n";

    // copy using push_back
    for (std::size_t i=0; i<=s.size(); ++i)
        v.push_back(s[i]);

    std::cout << "&v[0]:     " << &v[0] << "\n";
    std::cout << "begin/end: " << std::string(v.begin(), v.end()) << "\n";

    return 0;
}

Building and running this yields:

$ g++ main.cpp -o v && ./v
&v[0]:     some string
begin/end: 
&v[0]:     some string
begin/end: some string

My expectation was that it would print the string correctly in both cases, but assigning character by character using the index operator doesn’t print anything when later using begin() and end() iterators.

Why isn’t end() updated when when using []? If this is intentional, what’s the reason it’s working like this?

Is there a reasonable explanation for this behaviour? 🙂

I’ve only tried this with gcc 4.6.1 so far.

  • 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-28T22:10:46+00:00Added an answer on May 28, 2026 at 10:10 pm

    Typical example of Undefined Behavior.

    You are only ever allowed to access elements by index (using operator[]) between 0 and v.size()-1 (included).

    Using reserve does not modify the size, only the capacity. Would you have used resize instead, it would work as expected.

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

Sidebar

Related Questions

I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person
I found something interesting in jQuery, and I wanted to run it by some
I was looking through the std::vector code and I found something I didn't quite
I was looking through a code tutorial just now, and found something interesting --
I have a timer that ticks every 3 seconds. If the timer found something
I was debugging a stored procedure the other day and found some logic something
I found something related here but did not give me a good start Since
We found something similar to the following (don't ask ...): namespace N { struct
I was looking over some (C++) code and found something like this: //Foo.cpp namespace
While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass()

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.