Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - fixed size array in C struct

Tags:

c

struct

julia

I need to make a Julia type corresponding to a C struct that has a fixed size array:

struct cstruct {
    ...
    int arr[N] //N known at compile time
    ...
};

I have defined Julia types corresponding to other C structs with arrays like this:

type  jstruct
    ...
    arr::Ptr{Cint}
    ...
end

But as I understand it, this only works when arr is a pointer, not an array of a specific size. How can I ensure that the offsets of elements coming after arr remain the same in both languages?

like image 349
Matthew Bedford Avatar asked Oct 31 '16 14:10

Matthew Bedford


People also ask

Do arrays have fixed size in C?

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Is the size of array is fixed?

A fixed array is an array for which the size or length is determined when the array is created and/or allocated. A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages.

How do you initialize an array in Julia?

An Array in Julia can be created with the use of a pre-defined keyword Array() or by simply writing array elements within square brackets([]). There are different ways of creating different types of arrays.

How do you create an array of zeros in Julia?

This is usually called with the syntax Type[] . Element values can be specified using Type[a,b,c,...] . zeros([T=Float64,] dims::Tuple) zeros([T=Float64,] dims...) Create an Array , with element type T , of all zeros with size specified by dims .


1 Answers

When you define a C struct with a fixed size array (or with the array hack), the data are stored directly inline within that struct. It's not a pointer to another region. The equivalent Julia structure is:

type JStruct{N}
    arr::NTuple{N,Int}
end

That will store the integers directly inline within the struct.

like image 54
mbauman Avatar answered Sep 24 '22 14:09

mbauman