Skip to content

Passing struct by value to a foreign function does not work properly #342

@Apis035

Description

@Apis035

...And may crash the program. But passing by reference does work properly.

While passing struct by value:

  • With 1 field -> Ok
  • With 2 field -> Second field is zero
  • With 3 field -> Crash

Example C library:

typedef struct {int a, b, c} i3;

void checkByRef(i3 *s) { printf("%d, %d, %d\n", s->a, s->b, s->c); }
void checkByVal(i3 s)  { printf("%d, %d, %d\n", s.a, s.b, s.c); }

Adept:

record i3(a, b, c int)

foreign checkByRef(s *i3) void
foreign checkByVal(s i3) void

func main {
    v i3 = i3(1, 2, 3)
    checkByRef(&v) // Ok
    checkByVal(v)  // Crash
}

Test case

Related issue to discussion #337. Luckily, that problem can be resolved because the 4 ubyte struct is packed into a single int.

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