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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:50:58+00:00 2026-06-14T18:50:58+00:00

Running this on my setup: vector<int> myvector; for (int i=1; i<=5; i++) myvector.insert(myvector.end(),i); vector<int>::iterator

  • 0

Running this on my setup:

  vector<int> myvector;
  for (int i=1; i<=5; i++) myvector.insert(myvector.end(),i);

  vector<int>::iterator it;
  for ( it=myvector.begin() ; it < myvector.end()+2; it++ )
    cout << " " << *it;

Yields:

1 2 3 4 5 0 0

I would have thought that trying to dereference an out-of-range iterator would have resulted in a segfault. But it seems to yield an empty or default-initialized object of the type contained in the vector.

Is this well defined behaviour? And where does this property come from, from the iterator or from the vector? Does the iterator in some sense catch the out-of-range-exception and instead return an empty object?

I tried finding this in the C++11 reference, but it feels like it’s a bit over my head.

  • 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-14T18:50:59+00:00Added an answer on June 14, 2026 at 6:50 pm

    I would have thought that trying to dereference an out-of-range iterator would have resulted in a segfault.

    No, it gives undefined behaviour. The language does not require iterator accesses to be checked, as that would require a run-time check. C++ usually tries to avoid unnecessary run-time overhead, leaving the programmer to perform whatever checks are necessary.

    Most modern platforms use paged virtual memory, providing memory protection with a granularity of a few kilobytes. This means that there is often accessible memory after an allocated block (such as the one managed by std::vector), in which case out-of-range accesses will simply stomp on that memory.

    It may be possible to enable run-time iterator checks in your C++ library; alternatively, tools such as valgrind or efence can help to debug various memory errors, including out-of-range accesses.

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

Sidebar

Related Questions

First of all I have many Django instances setup and running like this. In
Totally confused by this one... We have a WAMPServer installation set up, running a
I am running wampserver on my windows vista machine. I have been doing this
Just wondering if this setup is possible: Java application running and monitoring embedded jetty
I'v got this setup currently running, where a website is running at site.domain.tld with
Running IIS 6, fastCGI, PHP 5.3.6 with PEAR MDB2. I've installed this setup multiple
I have just setup a OpenBSD little personal development server running PHP with an
Lets say we have this scenario a setup that deploys a .net com dll
I need advice if this setup is ok. I have a personal dev server
We have a setup in which EJB A is running on server A, and

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.