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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:01:03+00:00 2026-06-03T16:01:03+00:00

My colleague’s code looked like this: void copy(std::string const& s, char *d) { for(int

  • 0

My colleague’s code looked like this:

void copy(std::string const& s, char *d) {
  for(int i = 0; i <= s.size(); i++, d++)
    *d = s[i];
}

His application crashes and I think that it is because this accesses s out of range, since the condition should go only up to s.size() - 1.

But other guys next to me says there was a discussion in the past about this being legal. Can anyone please clear this up for me?

  • 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-06-03T16:01:04+00:00Added an answer on June 3, 2026 at 4:01 pm

    Let’s put aside the possiblity that *d is invalid since that has nothing to do with what the question seems directed at: whether or not std::string operator[]() has well defined behavior when accessing the “element” at index std::string::size().

    The C++03 standard has the following description of string::operator[]() (21.3.4 “basic_string element access”):

    const_reference operator[](size_type pos) const;
    reference operator[](size_type pos);
    

    Returns: If pos < size(), returns data()[pos]. Otherwise, if pos
    == size()
    , the const version returns charT(). Otherwise, the behavior is undefined.

    Since s in the example code is const, the behavior is well defined and s[s.size()] will return a null character. However, if s was not a const string, the behavior would be undefined.

    C++11 remedies this odd-ball behavior of the const version behaving so differently than the non-const version in this edge case. C++11 21.4.5 “basic_string element access” says:

    const_reference operator[](size_type pos) const;
    reference operator[](size_type pos);
    

    Requires: pos <= size().

    Returns: *(begin() + pos) if pos < size(), otherwise a reference
    to an object of type T with value charT(); the referenced value
    shall not be modified.

    So for a C++11 compiler, the behavior is well-defined whether or not the string is const.

    Unrelated to the question, I find it a little strange that C++11 says that “the referenced value shall not be modified” – it’s not clear to me if that clause applies only in the case where pos == size(). I’m pretty sure there’s a ton of existing code that does things like s[i] = some_character; where s is a non-const std:string and i < s.size(). Is that undefined behavior now? I suspect that that clause applies only to the special-case charT() object.

    Another interesting thing is that neither standard seems to require that the address of the object returned for s[s.size()] be in any way related to the address of the object returned for s[s.size() - 1]. In other words, it seems like the returned charT() reference doesn’t have to be contiguous to the end of the string data. I suspect that this is to give implementers a choice to just return a reference to a single static copy of that sentinel element if desired (that would also explain C++11’s “shall not be modified” restriction, assuming it applies only to the special case).

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

Sidebar

Related Questions

A colleague came across some code that looked like this and couldn't understand how
A colleague of mine refactored this code: private void btnGeneral_Click(object sender, RoutedEventArgs e) {
A colleague of mine asked me to write a homework for him. Although this
A colleague of mine is going a bit nuts with const in ANSI C
My colleague wrote the following stackoverflow question: other stack overflow question on this topic
A colleague told me that using the following method: string url = SomeURL; string
A colleague has passed me an interesting code sample that crashes with an InvalidProgramException
A colleague and I are working on the same area of code using Visual
My colleague insists on explicitly specifying the namespace in code as opposed to using
A colleague of mine wants to use my python 2 code on his OS

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.