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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:31:52+00:00 2026-06-06T10:31:52+00:00

Possible Duplicate: checking if pointer points within an array If I have an array

  • 0

Possible Duplicate:
checking if pointer points within an array

If I have an array and a size, and I want to check if a given pointer points to an element inside an array, is there any way to do so in standard C or C++ without invoking UB?

Does this work?

bool is_inside(someType * array, int size, someType * other_pointer){
    for (int i = 0; i < size; i++)
        if (array + i == other_pointer)
            return true;
    return false;
}

edit: It is my understanding that you cannot use comparisons other than == and != for pointers not pointing to the same array without UB (although in practice it works as expected). Was I wrong in this?

  • 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-06T10:31:52+00:00Added an answer on June 6, 2026 at 10:31 am

    It depends on what you mean by UB.

    Specifically, for pointer comparisons, section 5.9 “Relational Operators” of the C++ standard says:

    If two pointers p and q of the same type point to different objects that are
    not members of the same object or to different functions or if only one of
    them is null, the results of p<q, p>q, p<=q and p>=q are unspecified.

    Note that the behaviour is unspecified (meaning that the result of the comparison could be true or false – in other words the result doesn’t tell you anything useful – but the implementation isn’t required to specify which) as opposed to undefined (meaning that the compiler or the resulting program could do anything at all).

    However, so far I have only seen one family of implementations that didn’t do the expected thing with code like Kirill’s:

    bool inside = (other_pointer >= array) && (other_pointer < array+size);
    

    Those implementations are compilers intended for building MS-DOS real-mode programs, in which addresses have a paragraph and offset part. The addresses FF00:0010 and FF01:0000 point to the same memory location but if I recall correctly the compilers weren’t guaranteed to behave in the expected way, except when compiling for some memory models (certainly the HUGE model but perhaps others).

    However, if either p or q does not point to an existing object (because for example the pointer was freed) then the behaviour is going to be undefined no matter what you do. So you can’t use this kind of method to figure out if a pointer is still valid.

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

Sidebar

Related Questions

Possible Duplicate: Checking if array is multidimensional or not? How do I check whether
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: Checking for an SQL result in VB.NET I have login form which
Possible Duplicate: GCC STL bound checking Is there something like a automatic debug version
Possible Duplicate: Checking available stack size in C EDIT2: My question is Duplicate of
Possible Duplicate: Tool for checking unused CSS selectors? So, I'm wondering is there something
Possible Duplicate: checking if 2 numbers of array add up to I My Problem
Possible Duplicate: More concise way to check to see if an array contains only
Possible Duplicate: PHP - reversed order in if statement Checking for null - what

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.