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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:48:56+00:00 2026-05-11T21:48:56+00:00

I have seen it asserted several times now that the following code is not

  • 0

I have seen it asserted several times now that the following code is not allowed by the C++ Standard:

int array[5];
int *array_begin = &array[0];
int *array_end = &array[5];

Is &array[5] legal C++ code in this context?

I would like an answer with a reference to the Standard if possible.

It would also be interesting to know if it meets the C standard. And if it isn’t standard C++, why was the decision made to treat it differently from array + 5 or &array[4] + 1?

  • 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-11T21:48:56+00:00Added an answer on May 11, 2026 at 9:48 pm

    Your example is legal, but only because you’re not actually using an out of bounds pointer.

    Let’s deal with out of bounds pointers first (because that’s how I originally interpreted your question, before I noticed that the example uses a one-past-the-end pointer instead):

    In general, you’re not even allowed to create an out-of-bounds pointer. A pointer must point to an element within the array, or one past the end. Nowhere else.

    The pointer is not even allowed to exist, which means you’re obviously not allowed to dereference it either.

    Here’s what the standard has to say on the subject:

    5.7:5:

    When an expression that has integral
    type is added to or subtracted from a
    pointer, the result has the type of
    the pointer operand. If the pointer
    operand points to an element of an
    array object, and the array is large
    enough, the result points to an
    element offset from the original
    element such that the difference of
    the subscripts of the resulting and
    original array elements equals the
    integral expression. In other words,
    if the expression P points to the i-th
    element of an array object, the
    expressions (P)+N (equivalently,
    N+(P)) and (P)-N (where N has the
    value n) point to, respectively, the
    i+n-th and i−n-th elements of the
    array object, provided they exist.
    Moreover, if the expression P points
    to the last element of an array
    object, the expression (P)+1 points
    one past the last element of the array
    object, and if the expression Q points
    one past the last element of an array
    object, the expression (Q)-1 points to
    the last element of the array object.
    If both the pointer operand and the
    result point to elements of the same
    array object, or one past the last
    element of the array object, the
    evaluation shall not produce an
    overflow; otherwise, the behavior is
    undefined
    .

    (emphasis mine)

    Of course, this is for operator+. So just to be sure, here’s what the standard says about array subscripting:

    5.2.1:1:

    The expression E1[E2] is identical (by definition) to *((E1)+(E2))

    Of course, there’s an obvious caveat: Your example doesn’t actually show an out-of-bounds pointer. it uses a “one past the end” pointer, which is different. The pointer is allowed to exist (as the above says), but the standard, as far as I can see, says nothing about dereferencing it. The closest I can find is 3.9.2:3:

    [Note: for instance, the address one past the end of an array (5.7) would be considered to
    point to an unrelated object of the array’s element type that might be located at that address. —end note ]

    Which seems to me to imply that yes, you can legally dereference it, but the result of reading or writing to the location is unspecified.

    Thanks to ilproxyil for correcting the last bit here, answering the last part of your question:

    • array + 5 doesn’t actually
      dereference anything, it simply
      creates a pointer to one past the end
      of array.
    • &array[4] + 1 dereferences
      array+4 (which is perfectly safe),
      takes the address of that lvalue, and
      adds one to that address, which
      results in a one-past-the-end pointer
      (but that pointer never gets
      dereferenced.
    • &array[5] dereferences array+5
      (which as far as I can see is legal,
      and results in “an unrelated object
      of the array’s element type”, as the
      above said), and then takes the
      address of that element, which also
      seems legal enough.

    So they don’t do quite the same thing, although in this case, the end result is the same.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Heap space is NOT always set to zero. May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer One way to accomplish this is to create the GUI… May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer See Google Maps API Range Limiting for an article on… May 12, 2026 at 1:12 am

Related Questions

So I'm seeing some strange issues in ie8 with jquery, and assorted javascript files.
I have seen it written in multiple threads/comments on stackoverflow that using switch is
I know it is possible. I have seen it a couple of times, but
I know this can be done and i have seen it done using some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.