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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:36:41+00:00 2026-05-16T06:36:41+00:00

I need a way to get a pointer to the start of an object

  • 0

I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance.

I found this article which describes a way to do it (see the section called “Dynamic Casts”) using typeid and a dynamic_cast to void* in the case that T is a polymorphic type.

This works perfectly well on MSVC, however on GCC (4.x) it seems to fall on its arse and spits out a compiler error when it is used with a non-polymorphic type.

Does anyone know a way to:

  • Make GCC behave itself, and evaluate typeid correctly
  • Or another way to do this, that will compile on GCC

Below is the code I am currently using to try and achieve this.

template <typename T>
void* dynamicCastToVoidPtr(T *const ptr)
{
    // This is done using a separate function to avoid a compiler error on some 
    // compilers about non-polymorphic types when calling startOfObject
    return dynamic_cast<void*>(ptr);
}

template <typename T>
void* startOfObject(T *const ptr)
{
    // In cases of multiple inheritance, a pointer may point to an offset within 
    // another object
    // This code uses a dynamic_cast to a void* to ensure that the pointer value 
    // is the start of an object and not some offset within an object
    void *start = static_cast<void*>(ptr);
    if(start)
        typeid(start = dynamicCastToVoidPtr(ptr), *ptr);
    return start;
}

template <typename T>
void doSomethingWithInstance(T *const instance)
{
    // Here is where I need to get a void* to the start of the object
    // You can think of this as the deleteInstance function of my memory pool
    // where the void* passed into freeMemory should point to the
    // start of the memory that the memory pool returned previously
    void *start = startOfObject(instance);
    if(start)
        allocator->freeMemory(start);
}

Thanks.

  • 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-16T06:36:41+00:00Added an answer on May 16, 2026 at 6:36 am

    I’ve found a solution from another question that lets me work out at compile time if a type is polymorphic and I can then use this with template specialisation to use the correct type of cast. Apparently this method might break if the compiler adds padding between sub-objects, but I can hopefully add some compile time asserts on some known cases to catch that. It does compile and run correctly on both MSVC and GCC.

    This is the code to work out if a type is polymorphic.

    #define SIMPLE_POLYMORPHIC(TYPE, POLYMORPHIC)   \
        template <>                                 \
        struct IsPolymorphic<TYPE>                  \
        {                                           \
            static const bool value = POLYMORPHIC;  \
        };
    
    template <typename T>
    struct IsPolymorphic
    {
        struct Derived : public T { virtual ~Derived(); };
        static const bool value = (sizeof(Derived) == sizeof(T));
    };
    
    SIMPLE_POLYMORPHIC(int, false);
    SIMPLE_POLYMORPHIC(unsigned int, false);
    // ... do this for all intrinsic or non-derivable types
    

    The code to perform the casting based on whether the type is polymorphic.

    template <typename T, bool isPolymorphic = IsPolymorphic<T>::value>
    struct StartOfObject
    {
        static void* getStart(T *const ptr)
        {
            return static_cast<void*>(ptr);
        }
    };
    
    template <typename T>
    struct StartOfObject<T, true>
    {
        static void* getStart(T *const ptr)
        {
            if(ptr)
                return dynamic_cast<void*>(ptr);
            return NULL;
        }
    };
    

    And a test case for it.

    #define CLASS_STUFF(CLASS)      \
        public:                     \
            CLASS() {}              \
            virtual ~CLASS() {}     \
            int m_##CLASS;
    
    class A
    {
        CLASS_STUFF(A);
    };
    
    class B : public A
    {
        CLASS_STUFF(B);
    };
    
    class C
    {
    };
    
    #include <iostream>
    
    int main()
    {
        std::cout << IsPolymorphic<A>::value << std::endl;
        std::cout << IsPolymorphic<B>::value << std::endl;
        std::cout << IsPolymorphic<C>::value << std::endl;
        std::cout << IsPolymorphic<int>::value << std::endl;
    
        StartOfObject<A>::getStart(new A());
        StartOfObject<B>::getStart(new B());
        StartOfObject<C>::getStart(new C());
        StartOfObject<int>::getStart(new int());
    
        return 0;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find a way to get at the request/response streams inside of
I need a way to get a ( content ) MovieClip within a (
I need a quick easy way to get a string from a file in
I needed to find a way to get a pointer to a substring (like
Need a way to allow sorting except for last item with in a list.
I need a way to easily export and then import data in a MySQL
I need a way to recursively delete a folder and its children. Is there
I need a way to update the month value on a dateTime field in
I need a way to bind POJO objects to an external entity, that could
I need a way to build C++ code from UML diagrams and vice versa.

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.