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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:35:05+00:00 2026-05-24T07:35:05+00:00

TL;DR: I have a Derived** that I store as a in Lua as a

  • 0

TL;DR: I have a Derived** that I store as a in Lua as a void* userdata. Then I try to get it back as a Base** and stuff breaks. Is there anything I can do or is this all madness that’s doomed to failure?

Details:

I’m passing some data back and forth between Lua and C++, and Lua requires the use of void* to store userdata (That I’m using Lua isn’t too important, other than that it uses void pointers). Makes sense so far. Lets say I have three classes, Base and Derived, with Derived inheriting from Base. The userdata I feed to Lua is a pointer to a pointer, like so:

template <typename T>
void lua_push(L, T* obj) {
    T** ud = (T**)lua_newuserdata(L, sizeof(T*)); // Create a new userdata
    *ud = obj; // Set a pointer to my object
    // rest of the function setting up other stuff omitted
}

Of course, this is in a nice templated function, so I can pass any of my three types in this way. Later on I can use another templated function to get my userdata out of Lua, like so:

template <typename T>
T* lua_to(lua_State* L, int index) { 
    // there's normally a special metatable check here that ensures that 
    // this is the type I want, I've omitted it for this example
    return *(T**)lua_touserdata(L, index);
}

This works fine when I pass in and out the same type. I’m running into a problem though when trying to pull a Derived out as a Base.

In my specific case, I have a vector being stored on Base. I use lua_push<Derived>(L, obj); to push my object to Lua. Later, in another place I pull it out using Base* obj = lua_to<Base>(L, i);. I then push_back some stuff into my vector. Later on, another portion of code pulls out that exact same object (verified with pointer comparisons) except this time uses Derived* obj = lua_to<Derived>(L, i); My Derived object doesn’t see that object that was pushed in. I believe I’ve narrowed this down to incorrect casting, and I’m probably corrupting some memory somewhere when I make my call to push_back

So my question is, is there a way to make that cast work right? I’ve tried the various flavors of casts. static_cast, dynamic_cast and reinterpret_cast don’t seem to work, either giving me the same wrong answer or not compiling at all.

Specific example:

Base* b = lua_to<Base>(L, -1); // Both lua_to's looking at the same object
Derived* d = lua_to<Derived>(L, -1); // You can be double sure because the pointers in the output match
std::cout << "Base: " << b << " " << b->myVec.size() << std::endl;
std::cout << "Derived: " << d << " " << d->myVec.size() << std::endl;

Output:

Base: 0xa1fb470 1
Derived: 0xa1fb470 0
  • 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-24T07:35:08+00:00Added an answer on May 24, 2026 at 7:35 am

    The code is not safe. When you cast Base * to void *, you should always cast void * back to Base * first and then cast it again to Derived *. As so:

    Derived *obj = ...;
    Base** ud = reinterpret_cast<Base **>(lua_newuserdata(L, sizeof(Base*)));
    *ud = obj; // implicit cast Derived -> Base
    ...
    Derived *obj = static_cast<Derived *>(*ud); // explicit Base -> Derived
    

    Basically speaking,

    Y -> X -> void* -> X -> Y (safe)
    Y -> X -> void* -> Y (unsafe)
    

    The reason for this is that the actual pointer value of two pointers pointing to the same object may be different if the two pointers have different types. Whether it works depends on various factors such as inheritance and virtual functions. (It always works in C since C doesn’t have those facilities.)

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

Sidebar

Related Questions

I have a base class and 4 derived classes. I store all my derived
I have a (derived) Menu control, that displays a rather large list of items
I have multiple classes that all derive from a base class, now some of
When you have a derived class, is there an simpler way to refer to
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have developed a kind of brochure website template that I base most of
Is there a way in .NET to create a type derived from decimal that
I am attempting to store objects derived from a templated base class in an
I have some hierarchy: base, derived classes and some structure storing user data as
I have a base class, let's call it DispenseResult that stores the result of

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.