我无法确定此代码崩溃的原因:
#define MACOSX
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <ffi/ffi.h>
void sum(int64_t *a, int64_t *b, int64_t *c) {
*c = *a + *b;
}
int main() {
int64_t ai = 1, bi = 2, ci;
ffi_cif cif;
ffi_status status;
ffi_type *arg_types[] = {
&ffi_type_pointer,
&ffi_type_pointer,
&ffi_type_pointer,
};
void *args[] = {&ai, &bi, &ci};
status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &ffi_type_void, arg_types);
assert(status == FFI_OK);
ffi_call(&cif, FFI_FN(sum), NULL, args);
printf("%lld\n", ci);
return 0;
}
它因分段错误错误而失败。据我所知,ffi_call
如果ffi_prep_cif
用ffi_type_void
.
更新
lldb
输出:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x1)
frame #0: 0x0000000100000dd4 a.out`sum(a=0x0000000000000001, b=0x0000000000000002, c=0x00007fff5fbffac0) at main2.c:8
5 #include <ffi/ffi.h>
6
7 void sum(int64_t *a, int64_t *b, int64_t *c) {
-> 8 *c = *a + *b;
9 }
10
11 int main() {
因此,它在 void 函数内崩溃(尝试取消引用a
?,*
之前a
加了下划线)。