当使用 C 中的函数 ,makecontext
时<ucontext_t.h>
,它需要参数(ucontext_t* context, void (*someFunction)(void), int numberOfArguments, ...)
。numberOfArguments
是将参数传递给,someFunction
但是在传递参数时,我会收到编译警告。
我需要或应该删除这些,因为这是针对学校项目的,警告可能会让我丢分。我想我可以编辑 Makefile 的标志,但我更愿意调整代码。
我做了2次测试。
1:传递一个函数和字符串作为参数
test1 的代码是:
#include <stdio.h>
#include <ucontext.h>
// Outputs arg as a string
void* f(void* arg) {
printf("%s\n", (char*)arg);
return 0; // Returns back to c0
}
// makecontext takes as argument2, (void (*)(void)), but allows (void (*)(...))
// I want to remove the warning from using (void (*)(...))
void helper(void* (*selectFunction)(void*), void* selectArg) {
// Calls select function with select argument
selectFunction(selectArg);
}
int main()
{
// Initialize contexts
ucontext_t c0, c1;
char stack[1024];
getcontext(&c1);
// Setup stack for c1
c1.uc_stack.ss_sp = stack;
c1.uc_stack.ss_size = sizeof(stack);
c1.uc_link = &c0; // Swap to c0 stack on c1 stack end
void* (*selectFunction)(void*) = &f;
void* selectArg = (void*)("abc123 testing testing");
// Passes function f with arg "abc123 testing testing" to helper
makecontext(&c1, helper, 2, selectFunction, selectArg);
// Swap to context that calls function helper
swapcontext(&c0, &c1);
printf("Welcome back\n");
return 0;
}
我对 test1 的输出是:
$gcc -o main *.c
main.c: In function ‘main’:
main.c:33:22: warning: passing argument 2 of ‘makecontext’ from incompatible pointer type [-Wincompatible-pointer-types]
makecontext(&c1, helper, 2, selectFunction, selectArg);
^~~~~~
In file included from main.c:2:0:
/usr/include/ucontext.h:47:13: note: expected ‘void (*)(void)’ but argument is of type ‘void (*)(void * (*)(void *), void *)’
extern void makecontext (ucontext_t *__ucp, void (*__func) (void),
^~~~~~~~~~~
$main
abc123 testing testing
Welcome back
2 : 传递一个 int 作为参数
test2 的代码是:
#include <stdio.h>
#include <ucontext.h>
// Outputs arg as a string
void f(int n) {
printf("%d\n", n);
return; // Returns back to c0
}
// makecontext takes as argument2, (void (*)(void)), but allows (void (*)(...))
// I want to remove the warning from using (void (*)(...))
void helper(int arg) {
// Calls f with arg
f(arg);
}
int main()
{
// Initialize contexts
ucontext_t c0, c1;
char stack[1024];
getcontext(&c1);
// Setup stack for c1
c1.uc_stack.ss_sp = stack;
c1.uc_stack.ss_size = sizeof(stack);
c1.uc_link = &c0; // Swap to c0 stack on c1 stack end
// Make a context to run f with argument 5
makecontext(&c1, helper, 1, 5);
// Swap to context that calls function helper
swapcontext(&c0, &c1);
printf("Welcome back\n");
return 0;
}
我对 test2 的输出是:
$gcc -o main *.c
main.c: In function ‘main’:
main.c:30:22: warning: passing argument 2 of ‘makecontext’ from incompatible pointer type [-Wincompatible-pointer-types]
makecontext(&c1, helper, 1, 5);
^~~~~~
In file included from main.c:2:0:
/usr/include/ucontext.h:47:13: note: expected ‘void (*)(void)’ but argument is of type ‘void (*)(int)’
extern void makecontext (ucontext_t *__ucp, void (*__func) (void),
^~~~~~~~~~~
$main
5
Welcome back