Wrapping C struct with contiguous memory requirements

So I need to wrap the following data structure in c:

typedef struct {
    uint8_t type : 8;   
    uint8_t flags : 8;  
    uint64_t size : 48;
} Header;

where size corresponds to the length of a uint64_t* array following immediately afterwards;
I thought about doing something like

struct MyStruct
    data::Vector{UInt64}
    function MyStruct(n::Int)
        data = Vector{UInt64}(undef, n+1)
        header = #... construct header here ...
        data[1] = header
        return new(data)
   end 
end

is there any better way of doing this? e.g. as far as I understand

struct MyStruct2
    header::UInt64
    data::Vector{UInt64}
end

is not guaranteed to have the same memory layout: data may be stored as a pointer to the actual data and even if it is not, there is julia array header between header and the actual data.

Any advice on this?

I believe you are just going to have to use an Vector{UInt64} and treat the first element as the header. That is the only way I know of getting a sequential series of UInt64 values. So the:

struct MyStruct
    data::Vector{UInt64}
    function MyStruct(n::Int)
        data = Vector{UInt64}(undef, n+1)
        header = #... construct header here ...
        data[1] = header
        return new(data)
   end 
end

Will work. What I would do is then implemented functions like:

Base.setindex!(s::MyStruct, value, offset) = s.data[offset+1] = value
Base.getindex(s::MyStruct, offset) = s.data[offset + 1]

That will allow you to treat the object as an array to get/set the various elements. You might also want something like:

struct Header
    # header values.
    function Header(s::MyStruct) 
       # extract values from s.data[1]
       new(...)
    end
end

So you can easily get at the details about the header by just calling Header(myStruct).

edit: You probably also need to write a Base.cconvert function for MyStruct so that when you want to convert it to a Ref{UInt64} you actually perform the conversion on MyStruct.data…or you can just make sure your calls to C pass MyStruct.data instead of MyStruct.

I will have to study cconvert, thanks!