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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:49:55+00:00 2026-05-16T22:49:55+00:00

Possible Duplicate: Why am I able to make a function call using an invalid

  • 0

Possible Duplicate:
Why am I able to make a function call using an invalid class pointer

class B
{
    public:
    int i;
    B():i(0){}
    void func1()
    {
        cout<<“func1::B\n”;
    }
    void func2()
    {
        cout<<“i = “<<i;
    } 
};


int main()
{

    B *bp = new B;

    bp->func1();

    delete bp;

    bp = NULL;

    bp->func1();
    bp->func2();

    return 1; 
}

Output:

func1::B 
func1::B 
Runtime Exception:
NULL pointer access
  • 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-16T22:49:55+00:00Added an answer on May 16, 2026 at 10:49 pm

    This is the same old story of NULL (or invalid) object pointers; for the standard, calling a method on a NULL object pointer results in undefined behavior, which means that, as far as the standard is concerned, it could work perfectly fine, it could blow up the computer or kill some random people.

    What happens here is a consequence of the typical implementation of classes by C++ compilers: classes usually are actually structures that contain just the fields, and all the methods are actually functions which take as a hidden parameter the this pointer.

    Now, in this kind of implementation if you call a method with a NULL this pointer, if it doesn’t access any of the fields it won’t actually dereference this, so it should run fine (as happens with func1).

    If, instead, the method tries to access any of the fields (e.g. func2), it will dereference the this pointer, which, being NULL, will lead to a crash (dereferencing a NULL pointer it’s, again, undefined behavior, but usually it results in a crash).

    Note that if the methods that you’re calling are virtual it’s almost sure that calling them with a NULL this pointer will lead to a crash, since the virtual calls are resolved via the vtable (a function pointer array), which is hidden at the beginning of the class.

    By the way, void main() is not standard; it should be int main() (argv and argc are, instead, optional).

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

Sidebar

Related Questions

Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: trailing return type using decltype with a variadic template function I want
Possible Duplicate: Finding the Variable Name passed to a Function in C# public new
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: i want to make a web site which is able to control
Possible Duplicate: How to invalidate the file system cache? In order to be able
Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: Can I just make up attributes on my HTML tags? Hi, I
Possible Duplicate: How do I assign by “reference” to a class field in c#?

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.