# C like struct in numba

**URL:** <https://numba.discourse.group/t/c-like-struct-in-numba/139>\
**Category:** Community Support\
**Created:** [July 28, 2020, 4:03pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139 "2020-07-28T16:03:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 28, 2020, 4:03pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/1 "2020-07-28T16:03:22Z")

</div>

Hi guys,

Since there is no documentation about memory management in numba docs, I had to ask: Is there any way in numba to emulate C like structs without using @jitclass or TypedDicts? Class is highly experimental and dicts are slow by nature.

I know that you can emulate C like structures with np.dtypes like this:

```auto
struct_dtype = np.dtype([('row', np.float64), ('col', np.float64)])
ty = nb.from_dtype(struct_dtype)
ty_inst = np.arange(1, dtype=struct_dtype)

t[0]['row']

```

Is there any way to instantiate/allcate new structs with type ‘ty’ besides np.arange?  
Also, I want to be able to access it with dot notation, like this: ty\_inst.row

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [July 28, 2020, 4:30pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/2 "2020-07-28T16:30:39Z")

</div>

There may be a better solution (someone else may provide it or I may find something later), but one thing that springs to mind is that the Interval example for extending Numba - it implements an extension that represents an interval using a struct of two floats: [http://numba.pydata.org/numba-doc/latest/extending/interval-example.html](http://numba.pydata.org/numba-doc/latest/extending/interval-example.html)

The example addresses the two operations (initializing and using dot notation) by demonstrating:

- Lowering the constructor (with `@type_callable`) - this allows initializing the struct in a customized way. If you want to do the initialization outside a jitted function, the Python implementation of the class takes care of this - if you do it in a jitted function, the native code to initialize the struct is generated.
- Attribute access (with `make_attribute_wrapper`) - i.e. you can access attributes with the dot notation, instead of the getitem-like mechanism.

Based on this example, are you able to build an extension that would fit your needs?

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 28, 2020, 4:42pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/3 "2020-07-28T16:42:35Z")

</div>

@gmarkall I saw the interval example, but it’s not mutable. I need it to be mutable. The docs say that making it mutable is more complicated without explaininig how to do it…

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 28, 2020, 5:08pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/4 "2020-07-28T17:08:45Z")

</div>

Another way, is to use named tuples, but those are also immutable

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [July 28, 2020, 7:29pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/5 "2020-07-28T19:29:08Z")

</div>

I think Numpy records do what you want. Adjusting your example

```auto
ty_inst = np.rec.array(1, dtype=struct_dtype)[0]
t.row

```

I work a lot with records and I don’t know any way to instantiate directly, without creating an array and slicing it.

If you can live with the limitation of only working with numpy types (so no lists, etc) then this is a great way. Otherwise you have to use jitclass or the new StructRef ([https://github.com/numba/numba/pull/5993](https://github.com/numba/numba/pull/5993)), but both are experimental .

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 29, 2020, 7:42am UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/6 "2020-07-29T07:42:30Z")

</div>

I think you have a typo

you either:

```auto
ty_rec = np.recarray(1, dtype=struct_dtype)[0]

```

or

```auto
ty_arr = np.zeros(1, dtype=struct_dtype)
ty_rec = np.rec.array(ty_arr, dtype=struct_dtype)[0]

```

or view

```auto
ty_rec = ty_arr.view(np.recarray)

```

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [July 29, 2020, 10:56am UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/7 "2020-07-29T10:56:30Z")

</div>

I guess all 3 options end up in the same, don’t they?

did the answer help despite the typo?

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 29, 2020, 11:18am UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/8 "2020-07-29T11:18:49Z")

</div>

Yes it did. thanks. I am trying now the StructRef thing. I didn’t knew about that one because the official docs are not up to date

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [July 30, 2020, 1:11pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/9 "2020-07-30T13:11:16Z")

</div>

@luk-f-a do you have any idea about how to pass type definitions to StructRef fields?

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [July 30, 2020, 1:24pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/10 "2020-07-30T13:24:10Z")

</div>

sorry, I haven’t used them yet.

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [August 3, 2020, 9:58am UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/11 "2020-08-03T09:58:14Z")

</div>

@rpopovici to follow up on making things mutable, it seems to me that there’s a straightforward way to make the `Interval` example (and anything similar to it) mutable - however, I fear I may be missing some edge case or other issue, so I’ve created [this thread](https://numba.discourse.group/t/interval-example-why-do-mutable-types-need-more-sophisticated-data-models/150) with the addition to the example and a couple of questions in case it’s not as straightforward as I think it is.

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [August 18, 2020, 7:56pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/12 "2020-08-18T19:56:08Z")

</div>

Calling np.recarray() is not supported in njit mode, but if I call it from outside then it works and also I can pass the record as a parameter to njit functions. Why? Is there any way to create np.recarrays from njit functions?

```auto
import numpy as np
import numba as nb
from numba import typed, typeof, njit, int64, types

struct_dtype = np.dtype([('row', np.float64), ('col', np.float64)])
ty = nb.from_dtype(struct_dtype)
ty_inst = np.arange(1, dtype=struct_dtype)

ty_rec = np.recarray(1, dtype=struct_dtype)[0]
ty_rec.row = 11.5
print(ty_rec)

@njit
def test(rec):
    ty_arr = np.zeros(1, dtype=struct_dtype)

    ty_rec = np.recarray(1, dtype=struct_dtype)[0] # error here
    ty_rec.row = 11.5

    rec.col = -15.5
    print(rec)
    print(ty_arr)

test(ty_rec)

```

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [August 18, 2020, 9:18pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/13 "2020-08-18T21:18:12Z")

</div>

```auto
@njit
def test(rec):
    ty_arr = np.zeros(1, dtype=struct_dtype)

    ty_rec = np.zeros(1, dtype=struct_dtype)[0]   
    ty_rec.row = 11.5

    rec.col = -15.5
    print(rec)
    print(ty_arr)

```

AFAIK, there’s no distinction between structured arrays and record arrays in Numba.

---

<div class="post-metadata">

**Author:** ![wkerzendorf](https://avatars.discourse-cdn.com/v4/letter/w/da6949/32.png) [@wkerzendorf](https://numba.discourse.group/u/wkerzendorf)\
**Post date:** [October 12, 2020, 12:19pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/14 "2020-10-12T12:19:49Z")

</div>

We are having the same problem with the struct. We are using `jitclass` for now but wondering if generating thousands of these is slower than using something else. What were your findings?

---

<div class="post-metadata">

**Author:** ![rpopovici](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/rpopovici/32/86_2.png) [@rpopovici](https://numba.discourse.group/u/rpopovici)\
**Post date:** [October 12, 2020, 2:23pm UTC](https://numba.discourse.group/t/c-like-struct-in-numba/139/15 "2020-10-12T14:23:34Z")

</div>

The only options I know of for heap allocated data structures are:

- StructModel, stack allocated intended originaly for low level llvm. more info here [Interval example: Why do mutable types need more sophisticated data models?](https://numba.discourse.group/t/interval-example-why-do-mutable-types-need-more-sophisticated-data-models/150)
- StructRef, new experimental heap allocated
- jitclass heap allocated
- numpy.recarrays
- named tuple, immutable
