va_copy
#include <stdio.h>
#include <stdarg.h> void func(char* format, va_list ptr);
void print(char* format,...); void print(char* format,...)
{
va_list ptr;
va_list ptr_tmp;
va_start(ptr, format); va_copy(ptr_tmp,ptr); printf("print ptr: %p,%p\n",format, ptr); vfprintf(stdout, format, ptr);
vfprintf(stdout, format, ptr);
//vfprintf(stdout, format, ptr_tmp); va_end(ptr);
} int main()
{
int i=,j=;
char a[]="hello world";
print("aaaaasdfas"
"%d,%d,%s\n",i,j,a);
return ;
}
在print函数中,调用了两次vfprintf,在使用第二个vfprintf时出现:Segmentation fault
先看va_list的结构,在我的关于va_list问题博客中已经说明,http://www.cnblogs.com/chuanyang/p/5533927.html
发生段错误的原因是fp_offset和gp_offset发生了改变,如下图,所以使用va_copy,再调用一次vfprintf(stdout, format, ptr_tmp);就没有产生错误,因为能力有限,具体的原理还不是很清楚,希望帮助你理解

va_copy的更多相关文章
- 【转载】va_list 可变参数 简介 va_copy vprintf
[说明]本文转载自 smart 的文章 http://blog.sina.com.cn/s/blog_590be5290100qhxr.html 及百度百科 va_list是一个宏,由va_star ...
- va_start、va_arg、va_end、va_copy 可变参函数
1.应用与原理 在C语言中,有时我们无法给出一个函数参数的列表,比如: int printf(const char *format, ...); int fprintf(FILE *s ...
- xmpp
xmpp学习 下载: Openfire 服务器:Openfire 4.0.2 客户端:Spark 2.7.7 安装 Openfire安装: 根据提示一直下一步,服务器域名设置为:localhost(p ...
- variadic function 的使用
最近在看<the c programming language> K&R 7.3章 Variable-length Argument Lists 变长参数列表, 笔记一下用法 1 ...
- [ASM C/C++] C语言函数的可选性自变量
函数的可选性自变量 C语言允许定义自变量数量可变的函数,称为variadic函数.variadic函数需要固定数目的强制性自变量,后面是数量可变的可选性自变量. 也就是说必须至少有一个强制性 ...
- #include <stdarg.h>
名称描述相容 // 作用描述 va_start使va_list指向起始的参数 va_arg检索参数C89 va_end释放va_list va_copy拷贝va_list的内容 实例解析: #inc ...
- botbrew下写glib2程序
作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据General Public License,v3 ...
- C语言之函数可变参数
先上一段代码: #include<cstdarg> #include<iostream> #include<string> using namespace std; ...
- va_list深究
va_list深究 2011-04-21 21:06:11| 分类: C/C++|字号 订阅 VA函数(variable argument function),参数个数可变函数,又称可变参数 ...
随机推荐
- #import和@class的使用
#import #import 大部分功能和#include是一样的,但是他处理了重复引用的问题,不用再去自己进行重复引用处理. @class 用于声明一个类,告诉编 ...
- 自定义AlertDialog的样式
一.在XML中定义好要显示的AlertDialog的布局 二.在代码中创建alertdialog 对象 AlertDialog dialog = new AlertDialog.Builder(thi ...
- A - Ice_cream’s world III
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- robotframework接口测试初探2
python这个requests模块常被用来测试接口.使用RequestLibrary库测试之前,先来看下这个模块是怎样使用的 最简单的调用是 r=requests.get("http:// ...
- [ADB]ADB(Android Debug Bridge)简介及基础(不包含命令)
"Android Debug Bridge (adb) is a versatile command line tool that lets you communicate with an ...
- PHP memcache 安装问题
第一步:下载 memcached.exe 第二步:c:\window\system32\cmd.exe 管理员启动 第三步: memcached.exe -d start (错误) failed ...
- iOS 按钮点击变色
之前一直以为是要在selected状态下增加一个背景图片,效果是颜色变深,明明我的图片是变浅: 之后试了用hightlighted 成功. [_loginBtn setBackgroundImage: ...
- C语言数据结构之 简单选择排序
算法:设所排序序列的记录个数为n.i取1,2,-,n-1,从所有n-i+1个记录(Ri,Ri+1,-,Rn)中找出排序码最小的记录,与第i个记录交换.执行n-1趟 后就完成了记录序列的排序. 编译器: ...
- NSLog 自定义 屏蔽
1.如何自定义NSLog呢? 直接在工程的XXX_Prefix.pch中加入以下语句(就相当于在全局中定义了)#define NSLog NSLog(@"#%s##%d#",str ...
- Ubuntu下安装boost
今天开始安装配置Ubuntu开发环境(Ubuntu 12.04).在干活之前就预计到会遇到很多问题,但是没想到一开始就卡壳,可能是linux中各种包的依赖关系太复杂了,决定写个帖子记录一下,免得以后再 ...