__builtin_apply/__builtin_apply_args

目录

Variable-Length Argument Lists

How can I call a function with an argument list built up at run time?

wacky ideas

__builtin_apply_args

__builtin_apply_args - GCC 4.1.1

__builtin_apply 's size argument for constructing call in gcc?

示例代码


Variable-Length Argument Lists

Variable-Length Argument Lists

How can I call a function with an argument list built up at run time?

链接:Question 15.13

Q: How can I call a function with an argument list built up at run time?

A: There is no guaranteed or portable way to do this. If you're curious, this list's editor has a few wacky ideas 古怪的想法 you could try...

Instead of an actual argument list, you might consider passing an array of generic (void *) pointers. The called function can then step through the array, much like main() might step through argv. (Obviously this works only if you have control over all the called functions.)

(See also question 19.36.)

Additional links: “wacky ideas

wacky ideas

wacky ideas

Somehow I've never gotten around to writing the definitive essay on those wacky ideas''. Here are several messages I've written at various times on the subject of what I call theinverse varargs problem''; they summarize most of the ideas, and should at least get you started. (There is some overlap, because some of the later ones were written when I didn't have copies of the earlier ones handy.)

  • article posted to comp.unix.wizards and comp.lang.c 1989-06-04 链接
  • article posted to comp.lang.c 1992-07-14 链接
  • mail message sent 1993-03-07 to someone asking about the ``wacky ideas'' 链接
  • more recent ideas (1997-06-28) 链接
  • most recent ideas (2001-05-27) 链接

__builtin_apply_args

__builtin_apply_args
__builtin_apply

void * __builtin_apply (void (*function)(), void *arguments, size_t size);

__builtin_return

__builtin_apply_args - GCC 4.1.1

原文链接:https://gcc.gcc.gnu.narkive.com/WvwssETm/builtin-apply-args-gcc-4-1-1

__builtin_apply 's size argument for constructing call in gcc?

我想使用 gcc 的构造调用功能...

从文档:

— 内置功能:

void * __builtin_apply (void (*function)(), void *arguments, size_t size)

此内置函数使用参数和大小描述的参数副本调用函数。

参数的值应该是 返回的值__builtin_apply_args。参数大小指定堆栈参数数据的大小,以字节为单位。

此函数返回一个指向数据的指针,该数据描述如何返回函数返回的任何值。数据保存在堆栈上分配的内存块中。

计算适当的大小值并不总是那么简单。该值用于__builtin_apply计算应推入堆栈并从传入参数区域复制的数据量。

我的问题是如何知道这个 size 参数的大小?如果值太小或太大,会有什么后果?

示例代码

https://github.com/Rtoax/test/blob/master/c/__builtin_XXX/__builtin_apply_args-2.c

/**
 * https://gcc.gcc.gnu.narkive.com/WvwssETm/builtin-apply-args-gcc-4-1-1
 *	gcc-src/gcc/testsuite/gcc.dg/builtin-apply2.c
 */
#include <stdio.h>
#include <stdarg.h>

union func_union {
    void (*f1)();
    void (*f2)(char *fmt, ...);
};



void foo(char *name, int d, int e, int f, int g)
{
	printf("\nname = %s\n13 = %d\n72 = %d\n89 = %d\n5 = %d\n", name,d,e,f,g );
}
void foo2(char *fmt, ...)
{
	va_list va;

    va_start(va, fmt);

    vprintf(fmt, va);

    va_end(va);
}

void bar(char *name, ...)
{
	void **arg;
    void *rslt;
    
	arg = __builtin_apply_args();
    
	rslt = __builtin_apply(foo, arg, 24);
	rslt = __builtin_apply(foo2, arg, 24);
    
    __builtin_return (rslt);
}

int main(void)
{
	bar("eeee: %d %d %d %d\n", 13, 72, 89, 5);
	return 0;
}

上一篇:Mongo DB Study: first face with mongo DB


下一篇:解读2015年互联网UGC内容发展态势,安全事件频发