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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:40:34+00:00 2026-06-03T11:40:34+00:00

Possible Duplicate: Regular cast vs. static_cast vs. dynamic_cast Undefined, unspecified and implementation-defined behavior I

  • 0

Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
Undefined, unspecified and implementation-defined behavior

I am facing a strange issue. In the following snippet I define a class

   class NewClass
   {
      public:
         void Test()
         {
             cout<<"NewClass Test"<<endl;
         }
   };

In my main() method, I write:

        void main()
        {
           int *ptr = new int();
           NewClass *n = ((NewClass *)ptr);
           n->Test();
        }

and it displays “NewClass Test”. I dont understand how its possible to type-cast any pointer to NewClass and still have it working.

Thanks in advance!

  • 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-03T11:40:35+00:00Added an answer on June 3, 2026 at 11:40 am

    That’s static dispatch at work. this is really unnecessary in this case (e.g. it is not used or relied upon within NewClass::Test()).

    Casting as NewClass *n = ((NewClass *)ptr); is type conversion by address, and there is no type checking in this context. In other words, you’re not creating a new NewClass instance anywhere, you are simply treating the memory at the address specified by the int* as a NewClass*. This is a dangerous conversion which should be avoided. In the event you need to funnel an object through an address where there type safety is lost (e.g. void*), always be sure both ends know what is sent and received. Fortunately, erasing type safety is becoming less common.

    The results are undefined, but you should expect bad side effects in most cases and you should avoid reinterpreting data as such at all costs.

    In this case, the compiler likely inserted the results because it knew them. Furthermore, no error was exhibited because there is no actual dependence on the address or state of the object in this case: Test() does not rely on the state/data/members/dynamic methods/vtable of this.

    If you were to add members such as std::strings to NewClass and print those as well… you can expect things to blow up sooner than they do now 🙂

    In the event the dangers are not evident: This is an extrememly dangerous conversion — all the data backed by the int* is being reinterpreted as NewClass*, and all of its internal memory and structure (e.g. vtables and magic cookies) are reinterpreted accordingly. It won’t take long before your program seg faults, either by reading beyond the end of the allocation (int*), or by treating an int as a completely unrelated type — in this case, consider the memory layout of a class with a vtable or data, such as adding some std::strings to the NewClass, and reading from and writing to those members.

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

Sidebar

Related Questions

Possible Duplicate: Regular cast vs. static_cast vs. dynamic_cast I don't quite get when to
Possible Duplicate: Remove element of a regular array I have a method defined which
Possible Duplicate: How to escape text for regular expression in Java Is there a
Possible Duplicate: Regular Expressions: Is there an AND operator? I let my users enter
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: PHP Regular express to remove <h1> tags (and their content) I have
Possible Duplicate: Useful Regular Expression Tutorial Hello, I recently started coding in javascript. I
Possible Duplicate: php regular expression help? hi, i want to replace i like apple
Possible Duplicate: Reversing a regular expression in python I think I ran into a
Possible Duplicate: What is the best regular expression for validating email addresses? I want

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.