# Wrapping C struct with contiguous memory requirements

**URL:** <https://discourse.julialang.org/t/wrapping-c-struct-with-contiguous-memory-requirements/30269>\
**Category:** General Usage\
**Created:** [October 24, 2019, 1:34pm UTC](https://discourse.julialang.org/t/wrapping-c-struct-with-contiguous-memory-requirements/30269 "2019-10-24T13:34:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [October 24, 2019, 1:34pm UTC](https://discourse.julialang.org/t/wrapping-c-struct-with-contiguous-memory-requirements/30269/1 "2019-10-24T13:34:16Z")

</div>

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

```nohighlight
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

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [October 25, 2019, 1:37pm UTC](https://discourse.julialang.org/t/wrapping-c-struct-with-contiguous-memory-requirements/30269/2 "2019-10-25T13:37:28Z")

</div>

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:

```julia
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:

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [October 27, 2019, 10:55am UTC](https://discourse.julialang.org/t/wrapping-c-struct-with-contiguous-memory-requirements/30269/3 "2019-10-27T10:55:06Z")

</div>

> [@pixel27](#):
>
> 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!
