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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:01:20+00:00 2026-06-12T12:01:20+00:00

Possible Duplicate: Calling class method through NULL class pointer I was asked this question

  • 0

Possible Duplicate:
Calling class method through NULL class pointer

I was asked this question in the interview can someone answer it?

#include<string>
#include<iostream>
#include <stdio.h>

using namespace std;

class A
{
int k;
public:
     void f1()
    {

     int i;
     printf("1");

    }

     void f2()
    {

     k = 3;
     printf("3");

    }

};
class B
{
int i;
public:
    virtual void f1()
    {
    printf("2");
    scanf("%d",&i);
    }

};


int main()
{
    A* a = NULL;
    B* b = NULL;

    a->f1(); // works why?(non polymorphic)
    b->f1(); // fails why?(polymorphic)
            a->f2(); //fails why?
}

The last 2 cases are of polymorphic classes. The first case is a normal class .i understand that if i access i in f1 of A it will again give a runtime exception . but i am not getting why that happens

  • 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-12T12:01:22+00:00Added an answer on June 12, 2026 at 12:01 pm

    I agree with the other posts that this is undefined behavior, meaning anything can happen when executing the program, including “doing the right thing”.

    Now, let’s look at how the calls are implemented:

    a->f1() is a normal method call (non virtual). Most compilers will compile this in a similar way as the following code:

        class A { int i; }
        void f1(A* a) { int i; printf("1"); }
    

    Meaning the this pointer is actually handled like a parameter to the function (in practice there are frequently some optimizations about how the this pointer is handled, but that is irrelevant here). Now, since f1 doesn’t use the this pointer, the fact that it is null doesnt cause a crash.

    a->f2() will actually crash because it uses the this pointer: it updates this->k.

    The call to b->f1() is a virtual function call, and this is typically implemented using a virtual table lookup as b->vtable[0](). Since b is null, the dereference to read the virtual table crashes.

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

Sidebar

Related Questions

Possible Duplicate: Calling class method through NULL class pointer #include <iostream> using namespace std;
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: Creating an instance using the class name and calling constructor How can
Possible Duplicate: PHP Using a variable when calling a static method I've read through
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: How can I find the method that called the current method? I'd
Possible Duplicate: Calling a method named “string” at runtime in Java and C I
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
Possible Duplicate: Retrieving the calling method name from within a method (C#) I have

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.