- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我无法确定这段代码崩溃的原因:
#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_type_void
调用 ffi_prep_cif
,则 ffi_call
必须忽略返回值指针。
更新
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
?, *
before a
before underlined)。
最佳答案
您的间接级别有问题。您的 CIF 对于提供的函数是正确的,但您发送给它的实际参数(数组 args
的元素)不是。
参数数组的元素应该是指向参数值的指针。当函数参数是指针类型时,这意味着您必须将指针传递给适当的指针值。相反,您尝试直接传递实际值。因此,函数实现尝试取消引用 a
(1) 的整数值,就好像它是一个指针值一样。
对于具有您提供的签名和 CIF 的函数,对其的 FFI 调用可能如下所示:
int64_t ai = 1, bi = 2, ci;
int64_t *ap = &ai, *bp = &bi, *cp = &ci;
void *args[] = {&ap, &bp, &cp}; // <-- pointers to the (pointer) arguments
// ...
ffi_call(&cif, FFI_FN(sum), NULL, args);
关于c - 具有 void 函数的 libffi 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329462/
作为作业的一部分,我正在尝试创建一个用户级线程库,如 pthreads。 为了处理线程之间的上下文切换,我使用了“swapcontext”函数。在使用它之前,我必须使用“makecontext”函数创
我是一名初级 C++ 程序员,我正在 Linux 机器上编程。 我遇到了这个错误: cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(v
我知道,例如 void *(*myFuncName)(void*) 是一个函数指针,它接受并返回 void*。 这是一个有两个参数的指针吗?void 指针是该类型的另一个返回 void* 和 void
所以我被告知它们彼此几乎相同 void function1 (void(func)(int), int arg){ func(arg); } void function2 (void(*fun
我目前正在 GNU Radio 上开发一个 bloc,我想使用一个线程。该线程用于从 UDP 套接字获取数据,因此我可以在我的 GNU Radio 集团中使用它。 “一般工作”功能是执行所有信号和数据
我正在尝试在主函数中创建一个线程并通过我的线程调用另一个类的函数。 在 main.cpp 中: SocketHandler *callserver; pthread_t thread1; pthrea
我正在使用pthread 为我自己实现线程类。所以,我创建了 Thread 类如下: class Thread { public: Thread() { } virtual void*
我收到上述警告并理解它,但就是不知道如何解决它。我的代码在下面,但基本上我所做的是在结构中存储一个函数指针,并在我从 main.c 调用的另一个函数中初始化该结构。当我将代码与默认函数(即 free(
在我的 android 应用程序中,我在 doInBackground 中执行一些操作通过扩展 AsyncTask类(class)。 (我在这个类中执行任何 UI 都没用) 这是正确使用 AsyncT
我在 GNU 编译器集合中使用 C。所以我需要将函数指针传递给一个函数。现在有两种我想要处理的可接受的函数指针原型(prototype): void function(void); 和 void fu
我正在尝试使用“CameraManager”类创建一个新线程,但出现以下错误: cannot convert '*void(CameraManager:: * )(void*) to void*( *
我想构建一个可以隐藏线程创建的“IThread”类。子类实现“ThreadMain”方法并使其自动调用,如下所示: class IThread { public: void BeginThre
我不明白什么 void (**)(void *, const char *) /* ^^ why are there 2 asterisks here? 意思是,它是一个指向函数的指针,但我失败
我必须将“risposta”类型的参数“r”发送到函数 RispostaServer。编译器给我:invalid conversion void*(*)() to void*(*)(void*) 这是
所以我目前正在使用,或者至少正在尝试编写一个利用 this C pthread threadpool library. 的程序 值得注意的是 thpool.h 中的以下函数: int thpool_a
我正在尝试使用 void* 指针将不同的对象存储在一个全局表中。问题是如何取回 void* 对象。如果我有一个公共(public)基类,比如 Object ,我总是可以将 void* 指针存储为 Ob
我是一名 C 程序员(在 linux 上),但现在我有一个关于 C++ 的项目,并且有一个问题。 这里是示例代码 g_action.sa_sigaction = (void(*)(int,siginf
class Scoreget{ private: //some variables public: Scoreget(){ //
这个问题在这里已经有了答案: Is there a difference between foo(void) and foo() in C++ or C? (4 个答案) func() vs fun
我正在尝试使用 SDL 和 SDL_Mixer 为音频创建一个 C++ 应用程序,并且正在尝试遵循 this教程。但是,使用 SDL_Mixer 的 Mix_HookMusicFinished() 不
我是一名优秀的程序员,十分优秀!