0
typedef struct _StructWithCArray {
  char a[3];
  char b;
} StructWithCArray;

I represent the a[3] as a struct contain three char, but it will fail on iOS i386 simulator.

Anyone has any ideas how to handle this?


Here is a demo to show the bug. You should use a simulator not newer than iPhone 5 to run the BugReport project, and simulator newer than iPhone 5 seems has no problem in this case.

Karl
  • 665
  • 4
  • 19

3 Answers3

0

A struct containing 3 characters is not the same thing as an array of 3 characters. It's not even safely castable. You will have to do

StructWithCArray swc;
swc.a[0] = s.c1;
swc.a[1] = s.c2;
swc.a[2] = s.c3;

If you don't care the code is not portable and you know for a fact that the memory layout of the array and struct are the same, you can use memcpy to copy the 3 chars.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

libffi doesn't have a built-in notion of an array type, primarily because there is no way to pass an array to a function in C -- it always decays as a pointer. However, as you've found, this is not ideal, because you may still need to represent an array as an element of a structure. That is, it's an oversight in libffi.

The workaround for this is to simply add one element to the structure for each element of the array. In your case you would add three char fields. This works in general because an array has the same alignment as its elements.

The libffi documentation now explicitly mentions this approach.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
  • Thanks for your reply, I missed the doc before. I have tried to add a struct with three char, but when run it in a iOS i386 simulator, it just crashed. Then I found this workaround is not treated the same in this arch. If your want more info, I can attach a simple demo in the question. – Karl Apr 22 '17 at 14:47
  • Please file a libffi bug on github instead. – Tom Tromey Apr 22 '17 at 15:59
  • I have add a demo project. I will file a bug later. – Karl Apr 22 '17 at 16:14
0

What about this.

ffi_type* ffi_type_nullptr = 0;
ffi_type ffi_type_array;

ffi_type_array.alignment = ffi_type_uchar.alignment;
ffi_type_array.size = ffi_type_uchar.size * 3;
ffi_type_array.type = FFI_TYPE_STRUCT; // Trick.
ffi_type_array.elements = &ffi_type_nullptr; // Pointer to a null pointer.

ffi_type* members[2];
members[0] = & ffi_type_array;
members[1] = 0;

ffi_type ffi_type_T = {0, 0, FFI_TYPE_STRUCT, members};