Skip to content

Different layout in C++'s composition and inheritance #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
not-fl3 opened this issue Oct 28, 2017 · 1 comment
Closed

Different layout in C++'s composition and inheritance #1116

not-fl3 opened this issue Oct 28, 2017 · 1 comment

Comments

@not-fl3
Copy link

not-fl3 commented Oct 28, 2017

Input C/C++ Header

class A {
  double X;
  int Y;
};

class B : public A {
  int Z;
};

Bindgen Invocation

bindgen test.hpp

Actual Results

ERROR:bindgen::codegen::struct_layout: Calculated wrong layout for B, too more 4 bytes
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct B {
    pub _base: A,
    pub Y: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_B() {
    assert_eq!(
        ::std::mem::size_of::<B>(),
        16usize,
        concat!("Size of: ", stringify!(B))
    );

C++ demonstration:
https://gist.github.com/not-fl3/1e2d478b6c74230c313fb4e1716b937b
Size of class is different for composition and inheritance, also with different field's visibility.

Expected Results

Probably, correct represntation of c++ memory layout will be with embedded public fields from base class to derived class. Not actually sure about this.

@emilio
Copy link
Contributor

emilio commented Oct 28, 2017

Dupe of #380, thanks for the report though!

See there for details and a bit more analysis.

@emilio emilio closed this as completed Oct 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants