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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:55:03+00:00 2026-06-14T21:55:03+00:00

class CheckPointer { public: CheckPointer(int * mbeg, int * mend) : beg(mbeg), end(mend), curr(mbeg)

  • 0
class CheckPointer {
public:
    CheckPointer(int * mbeg, int * mend) :
        beg(mbeg), end(mend), curr(mbeg) {}

    // subscript operator
    int & operator[] (const size_t pos) {
        if (beg + pos < beg) {
            throw out_of_range("ERR: before beg!");
        }
        if (beg + pos >= end)
            throw out_of_range("ERR: end or past end!");
        return *(const_cast<int *>(beg + pos));
    }
private:
    const int * beg;
    const int * end;
    int * curr;
};

I have defined a subscript operator for the class CheckPointer. As the @param pos is of type size_t, I cannot check whether the user has passed a positive or negative value. Howerver I try to write the code to do the bound-check instead and it works :

        if (beg + pos < beg) {
            throw out_of_range("ERR: before beg!");
        }

I don’t know why it works… Could any one help me?

Thank you for considering my question!


For more information:

Environment: eclipse CDT, Ubuntu 10.04

test-code:

int iarr[6] = {1, 2, 3, 4, 5, 6};
CheckPointer cp(iarr, iarr+6);
// subscript
cout << cp[2] << endl;
cout << cp[5] << endl;
cout << cp[-2] << endl; // error: before beg

test-code_output:

terminate called after throwing an instance of 'std::out_of_range'
  what():  ERR: before beg!
3
6
  • 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-14T21:55:04+00:00Added an answer on June 14, 2026 at 9:55 pm

    This works because the negative value is cast to an unsigned value. Your machine architecture probably uses two’s-complement for negative values, so a value of -2 results in:

    0xfffffffe (assuming 32-bit)
    

    When you add this to beg, it wraps around and provided beg is greater than or equal to 2, has the effect of subtracting 2 from it. Thus, despite the index being extremely large, it has essentially performed a subtraction. If the value of beg was 1 or 0, the error would be “past end” instead.

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

Sidebar

Related Questions

class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
class X { int i; public: X() { i = 0; } void set(int
class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public
class C { public: C(C& c) { i = c.i; j = 100; }
class LinkTest{ public: LinkTest(){ } void start(){ List list; Node * n ; for(int
class matrix { int n; double **a; //the matrix public: matrix(int); ~matrix(); int getN();
class A { private: int a; public: A( int set ) { a =
class A attr_accessor :dab .... end Now I have an array of instances of
class A{ virtual int foo1(int a){ return foo1_1(a,filler(a)); } template<typename FunctionPtr_filler> int foo1_1(int a,
class Test { public static void main(String[] args) throws Exception { Test t =

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.