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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:12:18+00:00 2026-05-23T02:12:18+00:00

The following code compiles correctly and get the mysterious output: special Investment function 00000000

  • 0

The following code compiles correctly and get the mysterious output:

special Investment function
00000000

(Environment: C++ VS2010)

#include <iostream>
#include <vector>
using namespace std;

class Security {
public:
  virtual ~Security() {}
};

class Stock : public Security {};

class Investment : public Security {
public:
  void special() {
    cout << "special Investment function" << endl;
  }
};

int main() {
  Security* p = new Stock;
  dynamic_cast<Investment*>(p)->special();
  cout << dynamic_cast<Investment*>(p) << endl;
  return 0;
}

How could it be? Dereferencing a NULL pointer and get a “correct” output instead of crash?
Is it a special “characteristic” of VS2010?

Now I see. I did a test and it appears that dereferencing “this” in “special” function cause the program to crash.

Thanks for your help.

  • 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-23T02:12:19+00:00Added an answer on May 23, 2026 at 2:12 am

    Dereferencing a null pointer is undefined behavior – you can get unexpected results. See this very similar question.

    In this case Investment::special() is called in a non-virtual way, so you can think the compiler just creates a global function

    Investment_special_impl( Investment* this )
    

    and calls it passing a null this pointer as the implicit parameter.

    You should not rely on this.

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

Sidebar

Related Questions

The following code compiles correctly under VC++ 8 on XPSP3, but running it causes
The following source code compiles correctly with Visual Studio 2010: namespace NS { class
I wrote the following code: #include <iostream> class A { public: A(){ std::cout <<
How does the following code compile correctly, #include <stdio.h> #define stringer( x ) printf_s(
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
the following code compiles with Visual Studio 2008 but not with g++ on Mac
In Microsoft Oslo SDK CTP 2008 (using Intellipad) the following code compiles fine: module
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
How is it possible that following code even compiles? As far as I can

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.