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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:59:33+00:00 2026-05-11T10:59:33+00:00

I was experimenting with C++ and found the below code as very strange. class

  • 0

I was experimenting with C++ and found the below code as very strange.

class Foo{ public:     virtual void say_virtual_hi(){         std::cout << 'Virtual Hi';     }      void say_hi()     {         std::cout << 'Hi';     } };  int main(int argc, char** argv) {     Foo* foo = 0;     foo->say_hi(); // works well     foo->say_virtual_hi(); // will crash the app     return 0; } 

I know that the virtual method call crashes because it requires a vtable lookup and can only work with valid objects.

I have the following questions

  1. How does the non virtual method say_hi work on a NULL pointer?
  2. Where does the object foo get allocated?

Any thoughts?

  • 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. 2026-05-11T10:59:34+00:00Added an answer on May 11, 2026 at 10:59 am

    The object foo is a local variable with type Foo*. That variable likely gets allocated on the stack for the main function, just like any other local variable. But the value stored in foo is a null pointer. It doesn’t point anywhere. There is no instance of type Foo represented anywhere.

    To call a virtual function, the caller needs to know which object the function is being called on. That’s because the object itself is what tells which function should really be called. (That’s frequently implemented by giving the object a pointer to a vtable, a list of function-pointers, and the caller just knows it’s supposed to call the first function on the list, without knowing in advance where that pointer points.)

    But to call a non-virtual function, the caller doesn’t need to know all that. The compiler knows exactly which function will get called, so it can generate a CALL machine-code instruction to go directly to the desired function. It simply passes a pointer to the object the function was called on as a hidden parameter to the function. In other words, the compiler translates your function call into this:

    void Foo_say_hi(Foo* this);  Foo_say_hi(foo); 

    Now, since the implementation of that function never makes reference to any members of the object pointed to by its this argument, you effectively dodge the bullet of dereferencing a null pointer because you never dereference one.

    Formally, calling any function — even a non-virtual one — on a null pointer is undefined behavior. One of the allowed results of undefined behavior is that your code appears to run exactly as you intended. You shouldn’t rely on that, although you will sometimes find libraries from your compiler vendor that do rely on that. But the compiler vendor has the advantage of being able to add further definition to what would otherwise be undefined behavior. Don’t do it yourself.

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

Sidebar

Related Questions

what happens if the element I am binding jQuery code to is not found
I've been experimenting with variadic templates and argument forwarding. I think I've found some
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I'm currently experimenting with build script, and since I have an ASP.net Web Part
I'm experimenting with Linq and am having trouble figuring out grouping. I've gone through
I'm experimenting with VS 2008 Express edition and when I hit f1 on a
I'm experimenting with OpenSSL on my network application and I want to test if
I'm experimenting with internationalization by making a Hello World program that uses properties files
I've been experimenting with fulltext search lately and am curious about the meaning of

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.