Skip to content

Arrays in a C struct #33

@dmbates

Description

@dmbates

I noticed in reading the code that the Julia struct

struct ReadStatVariable
    readstat_types_t::Cint
    index::Cint
    name::Ptr{UInt8}
    format::Ptr{UInt8}
    label::Ptr{UInt8}
    label_set::Ptr{Void}
    offset::Int64
    width::Csize_t
    user_width::Csize_t
    missingness::ReadStatMissingness 
end

does not align with the C declaration in readstat,h

typedef struct readstat_variable_s {
    readstat_type_t         type;
    int                     index;
    char                    name[256];
    char                    format[256];
    char                    label[1024];
    readstat_label_set_t   *label_set;
    off_t                   offset;
    size_t                  storage_width;
    size_t                  user_width;
    readstat_missingness_t  missingness;
    readstat_measure_t      measure;
    readstat_alignment_t    alignment;
    int                     display_width;
} readstat_variable_t;

The problem is that the fixed-size char arrays, like name do not correspond to Ptr{UInt8} or Cstring in Julia. If you want the structs to align in memory you need to insert a Julia struct that has the correct width. The Clang package at one time did this by creating a Julia struct with 256 UInt8 fields and inserting that as the name field. However, that approach is rather tedious.

In this case there is no need to define the Julia struct because the memory is allocated in the C code and passed by reference. I can prepare a PR if you wish.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions