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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:31:16+00:00 2026-05-27T13:31:16+00:00

Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;

  • 0

Consider following piece of code

void foo( bool forwad )
{
    vector<MyObject>::iterator it, end_it;
    int dir;
    
    it = some_global_vector.begin() + some_position;
    if( forward )
    {
        dir = 1;
        it += 1;
        end_it = some_global_vector.end();

    }
    else
    {
        dir = -1;
        it -= 1;
        end_it = some_global_vector.begin()-1;
    }

    while( it != end_it )
    {
       if( do_domething() )
         break;

       it += dir;
    }
}

As you can see there is some doubt when forward == false because there is a subtraction from begin() and iterator it can be subtracted when it points at begin(). I can’t find anywhere if it is ok until I not dereference this bad pointing iterator).

EDIT

I read ISO C++ Standard and have some conclusions.
There is no promise that vector::begin() can’t internally point to memory at address 0, and I was thinking that it is the end, but all containers depend on standard allocator. This allocator depends on new operator. And also, there is no information that new will never return 0. But standard allocator depends also on delete operator and this operator is supposed to do nothing if you pass 0. So by this fact, new can’t return 0 because there will be no way to delete that pointer, and by that, non empty vector can’t return begin() that points to 0.

Conclusion:

If above is right decrementing iterator that points at vector::begin() should be safe, since internal memory of the vector is continuous.

Am I right?

ULTIMATE ANSWER

Even if it works now and will be working in the future it is undefined behavior according to the standard. If you do this, you are doing this on your own risk. See this simmilar question for more information.

  • 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-27T13:31:17+00:00Added an answer on May 27, 2026 at 1:31 pm

    You cannot decrement an iterator passed begin, or compute begin() - 1.

    While the implementation is required to have a position for one passed the last element, it is not required to have any address space available before begin. So begin() - 1 might not be a valid address (and definitely not a valid iterator).


    On question number 2:

    Even though if (p == 0) tests if the pointer is null, that doesn’t mean that a null pointer has to be represented by all bits zero. It could also be all bits 1, or something else. Compiler magic will make the test work anyway.

    Another example of an invalid address is that when you deallocate a large block of memory, the heap manager just could possibly also remove the corresponding virtual address space from your process.

    Another memory block starting just after the deallocated space could then have an address, say, 0x10000 where the address 0x10000 - 1 does no longer exist. Some hardware, which uses dedicated address registers for pointers, is known to trap when loading an invalid pointer. It just could detect that 0x10000 - 1 isn’t mapped to RAM anymore and abort your program. The standard is written to allow this, because such hardware exists.

    We don’t say that this is what normally happens on common desktop operating systems, just what could happen according to the language standard.

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

Sidebar

Related Questions

Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == foo) {
Consider the following code piece: ... int N,var; vector<int> nums; cin >> N; while
Consider the following piece of Java code. int N = 10; Object obj[] =
Consider the following piece of code : <select> <option value=0>foo</option> <option value=1 selected=selected>bar</option> </select>
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Consider the following piece of code: void MyRunningThread() { while(counter>0) // counter is a
Consider the following piece of code: case class User(id: Int, name: String) object User{
Consider the following piece of code: class foo { private function m() { echo
Consider the following piece of LaTeX code: \begin{tabular}{p{1in}p{1in}} A & B\\ C & D\\
Consider following program: static void Main (string[] args) { int i; uint ui; i

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.