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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:35:13+00:00 2026-05-20T18:35:13+00:00

Is this example code valid? #include<vector> using namespace std; int main() { vector<int> vec(10);

  • 0

Is this example code valid?

#include<vector>
using namespace std;

int main() {
  vector<int> vec(10); // create with 10 elements
  vec.reserve(100);    // set capacity to 100
  vector<int>::iterator iter = vec.end(); // points 1 past vec[9]

  vec.push_back( 777 );

  bool is_this_valid_and_true =  *iter == vec[10]; // ?

  // VS2010 runtime error in debug build:
  // Expression: vector iterator not dereferencable
  // Works in release build

  iter = vec.end() + 1; // points 2 past vec[10]?
  vec.push_back( 888 );
  vec.push_back( 999 );

  is_this_valid_and_true =  *iter == vec[12]; // ?
}

The error in VS2010 may be related to this bug.

If I set the command line option /D_HAS_ITERATOR_DEBUGGING=0 or set

#define _HAS_ITERATOR_DEBUGGING 0
#include<vector>

there is no error.

Edit:

In light of the answers, I think this code should cause an error. There is no bug in the compiler. It only works in release mode because iterators are implemented as pointers.

  • 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-20T18:35:14+00:00Added an answer on May 20, 2026 at 6:35 pm

    You are thinking of iterators as pointers.

    Iterators may use pointers as an implementation detail but they are not actual pointers.

    Thus:

    iter = vec.end() + 1; // This is not valid.
    

    There is no such thing as two elements passed the end of data.

    vec.end() returns an iterator that when decrement is a reference to the last elements (assuming there are elements). While an iterator referencing the last element when incremented is equivalent to the iterator returned by end().

    // Note: Assuming vector iterators were not invalidated after an insert anyway.
    //       But for arguments sake lets play this out.
    //
    vector<int>::iterator iter = vec.end(); // points 1 past vec[9]
    vec.push_back( 777 );
    bool is_this_valid_and_true =  *iter == vec[10]; // Not valid.
                                                     // This is the end() iterator
                                                     // de-referencing it is UB
    

    But de-referenceing the iterator representing by end() is undefined behavior() (even if you have reserved space) (the implementation may not be using pointers. For example some of the DeBug STL implementations will do extensive error checking in the iterator code).

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

Sidebar

Related Questions

Many C++ books contain example code like this... std::cout << Test line << std::endl;
I'm trying to convert dates for example 30/12/2000 to 2000-12-30 using this code: System.Globalization.CultureInfo
There is this example code, but then it starts talking about millisecond / nanosecond
take a look at this example code: public class Comment { private Comment() {
The questions says everything, take this example code: <ul id=css-id> <li> <something:CustomControl ID=SomeThingElse runat=server
for example this code var html = <p>This text is <a href=#> good</a></p>; var
Has anyone done this / have some example code?
I see a lot of example code for C# classes that does this: public
I'll attempt to shorten this code example: public interface IThing { //... Stuff }
I've seen several examples of code like this: if not someobj: #do something But

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.