gpt4 book ai didi

c - 为什么 makecontext 的函数只接受整数参数

转载 作者:太空狗 更新时间:2023-10-29 12:25:25 25 4
gpt4 key购买 nike

makecontext 的手册页声明 argc 之后的参数应该仅为 integer(int):

...the function func is called, and passed the series of integer (int) arguments that follow argc

如果我们看一下 pthread_createclone 系统调用,它们有一个要传递给 func 的 void* 参数,以及一个struct 的指针可以包含用户希望拥有的任意数量的数据,这两个函数也不需要使用 va_args。所以我的问题是为什么 makecontext 不使用这种技术,即使用 void 指针,而不是 argc 和 va_args?

最佳答案

这是一个设计缺陷,导致了 makecontext 的消亡/删除。请参阅包含它的最新 POSIX 版本的合理文本:

http://pubs.opengroup.org/onlinepubs/009695399/functions/makecontext.html

With the incorporation of the ISO/IEC 9899:1999 standard into this specification it was found that the ISO C standard (Subclause 6.11.6) specifies that the use of function declarators with empty parentheses is an obsolescent feature. Therefore, using the function prototype:

void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);

is making use of an obsolescent feature of the ISO C standard. Therefore, a strictly conforming POSIX application cannot use this form. Therefore, use of getcontext(), makecontext(), and swapcontext() is marked obsolescent.

There is no way in the ISO C standard to specify a non-obsolescent function prototype indicating that a function will be called with an arbitrary number (including zero) of arguments of arbitrary types (including integers, pointers to data, pointers to functions, and composite types).

Replacing makecontext() with a number of ISO C standard-compatible functions handing various numbers and types of arguments would have forced all existing uses of makecontext() to be rewritten for little or no gain. There are very few applications today that use the *context() routines. Those that do use them are almost always using them to implement co-routines. By maintaining the XSH, Issue 5 specification for makecontext(), existing applications will continue to work, although they won't be able to be classified as strictly conforming applications.

There is no way in the ISO C standard (without using obsolescent behavior) to specify functionality that was standard, strictly conforming behavior in the XSH, Issue 5 specification using the ISO C standard. Threads can be used to implement the functionality provided by makecontext(), getcontext(), and swapcontext() but they are more complex to use. It was felt inventing new ISO C standard-compatible interfaces that describe what can be done with the XSH, Issue 5 functions and then converting applications to use them would cause more difficulty than just converting applications that use them to use threads instead.

请注意,为了使 makecontext 的实现真正调用 func,它必须知道如何设置堆栈帧以使用正确的函数入口参数的数量和类型。如果要求所有参数都具有相同的类型 (int),则可以仅从数字 argc 中知道这一点。如果允许它们具有不同的类型,调用者将不得不以某种方式将类型规范作为参数传递给 makecontext;一个简单的计数不足以知道如何将它们从 makecontext 接收的 va_list 复制到堆栈帧以进入 func

当然makecontextfunc应该只是取了一个void *,这样问题就不会出现了。但它没有。

关于c - 为什么 makecontext 的函数只接受整数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com