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

The Archive Base Latest Questions

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

My code was acting wonky and i was able to mini reproduce it with

  • 0

My code was acting wonky and i was able to mini reproduce it with the code below. (codepad link)

From http://www.cppreference.com/wiki/keywords/dynamic_cast

If you attempt to cast to a pointer
type, and that type is not an actual
type of the argument object, then the
result of the cast will be NULL.

From my understanding this_test should be null. It isnt. How do i check if that dummy ptr is actually a ptr to a dummy object?

#include <ios>
struct Dummy{ virtual void dummyfn(){} };

int main(){
Dummy* this_test = dynamic_cast<Dummy*>((Dummy*)0x123);
//assert(this_test==0);
cout << std::hex << this_test<<endl;
return 0;
}

output:

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

    The issue is that dynamic_cast expects either:

    • a null pointer
    • a valid pointer

    Here you can only offer it garbage, so it is useless, and not the cast you want.

    If you are getting a void*, then you can use reinterpret_cast (better than a C-cast, because more visible) to cast it into another type:

    void* p = 0x123;
    Dummy* dummy = reinterpret_cast<Dummy*>(p);
    

    Note: the presence or absence of virtual methods goes unnoticed here


    EDIT: if you can modify the objects being passed…

    Then try to use a common base class:

    struct Base: private boost::noncopyable { virtual ~Base() = 0 }; Base::~Base() {}
    

    And define the following helpers:

    template <typename T>
    void* to_void(T* t) {
      Base* base = t;
      return reinterpret_cast<void*>(base);
    }
    
    template <typename T>
    T* from_void(void* p) {
      Base* base = reinterpret_cast<Base*>(p);
      return dynamic_cast<T*>(base);
    }
    

    The former is extremely important because of the possible pointer adjustment (which will probably only occur in the case of Multiple Inheritance).

    Note: it’s possible to use a fast_cast here if you don’t use virtual inheritance or other RTTI stuff

    template <typename T, typename U>
    T* fast_cast(U* u) {
    #ifdef NDEBUG
      return static_cast<T*>(u);
    #else
      return dynamic_cast<T*>(u);
    #endif
    }
    

    If this is not possible the following solutions are possible, but they are going to feel hacky I fear.

    Since dynamic_cast is not going to work properly here, you have to actually come up with your own type checking mechanism.

    One method could be to use a “repository” in which you register the void* pointers you get, and the associated type_info object.

    typedef std::map<void*, std::type_info const*> Repository;
    
    template <typename Dest>
    Dest* dynamic_check(void* p, Repository const& rep) {
      Repository::const_iterator it = rep.find(p);
      assert(it != rep.end() && "dynamic_check: no such entry");
    
      assert(typeid(Dest) == *(it->second) && "dynamic_check: wrong type");
    
      return reinterpret_cast<Dest*>(p);
    }
    

    If this is not possible, then you could hack the C++ object model to your advantage. If you know that the object has at least one virtual method, then it necessarily has a virtual pointer on all compilers I know (VS, gcc, clang), and this pointer is the first 4/8 bytes of the object.

    inline void* virtual_pointer(void* p) {
      assert(p != 0 && "virtual_pointer: null");
      return reinterpret_cast<void*>(*p);
    }
    
    template <typename T>
    void* virtual_pointer(T const& t) {
      return virtual_pointer(reinterpret_cast<void*>(&t));
    }
    
    template <typename T>
    void* virtual_pointer() {
      static void* pointer = virtual_pointer(T());
      return pointer;
    }
    
    
    template <typename Dest>
    Dest* dynamic_check(void* p) {
      assert(virtual_pointer<Dest>() == virtual_pointer(p));
      return reinterpret_cast<Dest*>(p);
    }
    

    Note: both solutions suffer from the same shortcoming, they will only work if you precise the exact type (well, you could get away with it as long as two types share the same virtual table, which happens if a derived class does not override any virtual method, including the destructor).

    This is far from the power of a true dynamic_cast.

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

Sidebar

Related Questions

Code for server: http://stikked.com/view/64826511 Network Code for client: http://stikked.com/view/38974838 Basically, the client connects to
//CODE $entries = $model->fetchEntries(); $feedUri = '/rss/'; //link from which feed is available $link
I have some code acting as an HTTP client which supports basic authentication as
anyone can explain me, why this parts of code are acting differently? while((c =
Some assertions in my code just started acting strangely, and I was wondering if
All, I've got the following code to open some divs that are acting as
I know you're not supposed to include slow-acting code on your UI thread in
I have the css code below along with an image to show it's output.
Is SearchString acting as property of Page class? Here is code, <%@ Page Language=C#
For some reason this PHP code below will not work, I can not figure

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.