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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:56:36+00:00 2026-05-27T00:56:36+00:00

Inspired by this answer about dynamic cast to void* : … bool eqdc(B* b1,

  • 0

Inspired by this answer about dynamic cast to void*:

...
bool eqdc(B* b1, B *b2) {
    return dynamic_cast<void*>(b1) == dynamic_cast<void*>(b2);
}
...
int main() {
    DD *dd = new DD();
    D1 *d1 = dynamic_cast<D1*>(dd);
    D2 *d2 = dynamic_cast<D2*>(dd);
    ... eqdc(d1, d2) ...

I am wondering if it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid, but different objects.

More generally, but possibly not as relevant, is comparing (==or !=) two values of type void* always defined, or is it required that they hold a pointer to a valid object/memory area?

  • 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-27T00:56:36+00:00Added an answer on May 27, 2026 at 12:56 am

    C says:

    Two pointers compare equal if and only if both are null pointers, both are pointers to the
    same object (including a pointer to an object and a subobject at its beginning) or function,
    both are pointers to one past the last element of the same array object, or one is a pointer
    to one past the end of one array object and the other is a pointer to the start of a different
    array object that happens to immediately follow the first array object in the address
    space.

    C++ says:

    Two pointers of the same type compare equal if
    and only if they are both null, both point to the same function, or both represent the same address.

    Hence it would mean that:

    a)

    it is fully defined behaviour in C++ (according to the 03 or 11 standard) to compare two void pointers for (in)equality that point to valid, but different objects.

    So yes, in both C and C++. You can compare them and in this case they shall compare as true iff they point to the same object. That’s simple.

    b)

    is comparing (==or !=) two values of type void* always defined, or is it required that they hold a pointer to a valid object/memory area?

    Again, the comparison is well-defined (standard says “if and only if” so every comparison of two pointers is well-defined). But then…

    • C++ talks in terms of “address”, so I think this means that the standard requires this to work “as we’d expect”,
    • C, however, requires both the pointers to be either null, or point to an object or function, or one element past an array object. This, if my reading skills aren’t off, means that if on a given platform you have two pointers with the same value, but not pointing to a valid object (e.g. misaligned), comparing them shall be well-defined and yield false.

    This is surprising!

    Indeed that’s not how GCC works:

    int main() {
        void* a = (void*)1; // misaligned, can't point to a valid object
        void* b = a;
        printf((a == b) ? "equal" : "not equal");
        return 0;
    }
    

    result:

    equal
    

    Maybe it’s UB in C to have a pointer which isn’t a null pointer and doesn’t point to an object, subobject or one past the last object in an array? Hm… This was my guess, but then we have that:

    An integer may be converted to anypointer type. Except as previously specified, the
    result is implementation-defined, might not be correctly aligned, might not point to an
    entity of the referenced type, and might be a trap representation.

    So I can only interpret it that the above program is well-defined and the C standard expects it to print “not equal”, while GCC doesn’t really obey the standard but gives a more intuitive result.

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

Sidebar

Related Questions

Inspired by this question and answer , how do I create a generic permutations
This question is inspired by Jon Skeet's answer: Is there a c# equivalent to
Inspired by this answer .. Can you guys point me to something similar--something that
This question was inspired by this answer to another question, indicating that you can
G'day, Inspired by a comment I made in my answer to this question on
This question was inspired by this answer . I've always been of the philosophy
This is inspired by this question and the comments on one particular answer in
Inspired by the top answer on this page I wrote a python program to
This question is inspired from the accepted answer here . Why does the following
inspired by this answer , I wanted to inquire what other useful movement/editing commands

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.