Fork me on GitHub
Credit and license

Interfacing Fortran, C, C++, and Python: Memory allocation for arrays that pass the API

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • Where and how should we allocate memory?

Memory allocation for arrays that pass the API

Library side

Advantage:

  • Caller does not need to know the array sizes a priori.

Disadvantage:

  • Risk of a memory leak (garbage collection on the Python side).

Advantages:

  • Let the caller decide how memory is allocated.
  • Reduced risk of a memory leak.

Disadvantage:

  • You need to know array sizes.
  • Requires extra layer on the Python side.

Example

Consider these two functions:

double *get_array_leaky(const int len)
{
    double *array = new double[len];

    for (int i = 0; i < len; i++)
    {
        array[i] = (double)i;
    }

    return array;
}

void get_array_safe(const int len, double array[])
{
    for (int i = 0; i < len; i++)
    {
        array[i] = (double)i;
    }
}

How can we use the second one?

from cffi import FFI

def get_array_safe(length):
    ffi = FFI()
    lib = ffi.dlopen(...)

    array = ffi.new("double[]", length)
    lib.get_array_safe(length, array)

    return array

Questions

  • How can you make the API function return a Python list or dict?
  • Can you implement generators?

Key points

  • Allocate API arrays client-side.

  • Write a thin Python layer around them.