ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

__builtin_apply/__builtin_apply_args

2021-07-15 14:33:03  阅读:444  来源: 互联网

标签:__ gcc void args builtin apply


目录

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;
}

标签:__,gcc,void,args,builtin,apply
来源: https://blog.csdn.net/Rong_Toa/article/details/118758211

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有