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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:26:17+00:00 2026-05-25T01:26:17+00:00

I have a C library that has types like this: typedef struct { //

  • 0

I have a C library that has types like this:

typedef struct {
  // ...
} mytype;

mytype *mytype_new() {
  mytype *t = malloc(sizeof(*t));
  // [Initialize t]
  return t;
}

void mytype_dosomething(mytype *t, int arg);

I want to provide C++ wrappers to provide a better syntax. However, I want to avoid the complication of having a separately-allocated wrapper object. I have a relatively complicated graph of objects whose memory-management is already more complicated than I would like (objects are refcounted in such a way that all reachable objects are kept alive). Also the C library will be calling back into C++ with pointers to this object and the cost of a new wrapper object to be constructed for each C->C++ callback (since C doesn’t know about the wrappers) is unacceptable to me.

My general scheme is to do:

class MyType : public mytype {
 public:
   static MyType* New() { return (MyType*)mytype_new(); }
   void DoSomething(int arg) { mytype_dosomething(this, arg); }
};

This will give C++ programmers nicer syntax:

// C Usage:
mytype *t = mytype_new();
mytype_dosomething(t, arg);

// C++ Usage:
MyType *t = MyType::New();
t->DoSomething(arg);

The fib is that I’m downcasting a mytype* (which was allocated with malloc()) to a MyType*, which is a lie. But if MyType has no members and no virtual functions, it seems like I should be able to depend on sizeof(mytype) == sizeof(MyType), and besides MyType has no actual data to which the compiler could generate any kind of reference.

So even though this probably violates the C++ standard, I’m tempted to think that I can get away with this, even across a wide array of compilers and platforms.

My questions are:

  1. Is it possible that, by some streak of luck, this does not actually violate the C++ standard?
  2. Can anyone think of any kind of real-world, practical problem I could run into by using a scheme like this?

EDIT: @James McNellis asks a good question of why I can’t define MyType as:

class MyType {
 public:
  MyType() { mytype_init(this); }
 private:
  mytype t;
};

The reason is that I have C callbacks that will call back into C++ with a mytype*, and I want to be able convert this directly into a MyType* without having to copy.

  • 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-25T01:26:17+00:00Added an answer on May 25, 2026 at 1:26 am

    Is it possible that, by some streak of luck, this does not actually violate the C++ standard?

    I’m not advocating this style, but as MyType and mytype are both PODs, I believe the cast does not violate the Standard. I believe MyType and mytype are layout-compatible (2003 version, Section 9.2, clause 14: “Two POD-struct … types are layout-compatible if they have the same number of nonstatic data members, and corresponding nonstatic data members (in order) have layout-compatible types (3.9).”), and as such can be cast around without trouble.

    EDIT: I had to test things, and it turns out I’m wrong. This is not Standard, as the base class makes MyType non-POD. The following doesn’t compile:

    #include <cstdio>
    
    namespace {
        extern "C" struct Foo {
            int i;
        };
        extern "C" int do_foo(Foo* f)
        {
            return 5 + f->i;
        }
    
        struct Bar : Foo {
            int foo_it_up()
            {
                return do_foo(this);
            }
        };
    }
    
    int main()
    {
        Bar f = { 5 };
        std::printf("%d\n", f.foo_it_up());
    }
    

    Visual C++ gives the error message that “Types with a base are not aggregate.” Since “Types with a base are not aggregate,” then the passage I quoted simply doesn’t apply.

    I believe that you’re still safe in that most compilers will make MyType layout-compatible with with mytype. The cast will “work,” but it’s not Standard.

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

Sidebar

Related Questions

I have a ASP.NET intranet application that has a document library section. The user
I have a library that I would like to license and distribute. I know
I have this small code library that I'm considering releasing into Open Source. I
Whenever I have a library that use across different websites/ applications I've always just
I have a C++ library that provides various classes for managing data. I have
I have a shared library that I wish to link an executable against using
We have a C++ library that we provide to several different clients. Recently we
I have a java library that I am accessing in VB.NET via COM. The
I have a third party library that internally constructs and uses the SqlConnection class.
I have an existing library (JPhysX) that is a Java wrapper for a native

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.