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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:12:56+00:00 2026-05-29T16:12:56+00:00

Possible Duplicate: When does invoking a member function on a null instance result in

  • 0

Possible Duplicate:
When does invoking a member function on a null instance result in undefined behavior?

Anything like this:

class Class {
public:
    void Method()
    {
       //empty;
    }
};

Class* object = 0;
object->Method();

is undefined behavior in C++ because calling non-static member functions via null pointers is formally illegal. See this answer for a detailed explanation full of quotes from the C++ Standard. I’m well aware of the theoretical part and this question is not about theory and so it’s not a duplicate of that question.

In all implementations I’m aware of the code above or some equivalent thereof doesn’t cause any observable problems – since the member function doesn’t access the object the method will be called just fine.

May I have any real-life example in which the same setup causes practical observable problems?

  • 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-29T16:12:57+00:00Added an answer on May 29, 2026 at 4:12 pm

    Simple:

    struct Object { void foo(); std::string s; };
    
    void print(Object* o) {
      o->foo();
      if (o) { std::cout << o->x << "\n"; }
    }
    

    Let us say that foo does not access any non-static attribute of Object (ie, x).

    The problem is that because formally o->foo() is undefined behavior if o is null, then it is obvious that o is not null. Therefore the check is redundant.

    The function is thus optimized:

    void print(Object* o) {
      o->foo();
      std::cout << o->x << "\n";
    }
    

    Reversing the order does not change anything:

    void print(Object* o) {
      if (o) { std::cout << o->x << "\n"; }
      o->foo();
    }
    

    is still optimized:

    void print(Object* o) {
      std::cout << o->x << "\n";
      o->foo();
    }
    

    Sometimes referred to as the Time Travel Clause of Undefined Behavior by some SO members.

    For more information, check out Chris Lattner’s serie on Undefined Behavior:

    • What Every C Programmar Should Know About Undefined Behavior 1/3
    • What Every C Programmar Should Know About Undefined Behavior 2/3
    • What Every C Programmar Should Know About Undefined Behavior 3/3

    Your specific concern is addressed in 2/3.

    Whether this actually fails depend on the compiler you use, the optimization passes you specify and the order in which they run.

    Do you really want to depend on all that 😡 ?

    Of course, one would argue that’s it is pointless to have a function member that does not access any state of the object… so the question itself is of little value in practice (but interesting for its theoretical aspects).

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

Sidebar

Related Questions

Possible Duplicate: When does invoking a member function on a null instance result in
Possible Duplicate: why does initializing subclasses require calling the super class's same init function?
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript ?
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: Does every thread need its own autorelease pool? I would like to
Possible Duplicate: Does Java guarantee that Object.getClass() == Object.getClass()? If I have a class
Possible Duplicate: What does ||= mean? In this previous question, I asked about an
Possible Duplicate: How does this bash fork bomb work? Today one of my friend
Possible Duplicate: Why does this() and super() have to be the first statement in

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.