Click here to Skip to main content
Licence CPOL
First Posted 29 Mar 2005
Views 73,628
Bookmarked 15 times

va_list, va_start, va_pass!!! or how to pass variable-argument list to next va-function

By | 29 Mar 2005 | Article
trick to pass variable-argument list to next va-function

Introduction

I've been looking for solution of passing variable-argument list from my va-function to another one, like TRACE for example. All solutions I saw were about using special functions that take va_list as argument. But this is a un-straight way. Why couldn't I just pass "..." to next function? C++ syntax doesn't allow this. But C++ allows to extend itself. Let me introduce you new macros from va_ set:

template<byte count>
struct SVaPassNext{
    SVaPassNext<count-1> big;
    DWORD dw;
};
template<> struct SVaPassNext<0>{};
//SVaPassNext - is generator of structure of any size at compile time.

class CVaPassNext{
public:
    SVaPassNext<50> svapassnext;
    CVaPassNext(va_list & args){
		try{//to avoid access violation
			memcpy(&svapassnext, args, sizeof(svapassnext));
		} catch (...) {}
    }
};
#define va_pass(valist) CVaPassNext(valist).svapassnext

#if 0 //using:
void MyVA_Function(szFormat, ...){
    va_list args;
    va_start(args, szFormat);
    SomeOtherVA_Function(szFormat, va_pass(args));
    va_end(args);
}
#endif
how this works:
I just copy 50 * sizeof(DWORD) bytes of stack to my struct of this size and simply pass this struct as ... argument to next function. Compiler just copies my copy of local stack to next function stack. And that's all we need.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

araud

Software Developer (Senior)

Russian Federation Russian Federation

Member

Real christian since 1997 (to be a cristian means to follow Jesus Christ)
 
Programmer since 1999
main language is C++ and I fond of it!
 
Married to beautiful and clever sister in Christ since 2001
 
Musician lead guitar - since 1995, vocal since 2004. I play neoclassic-doom-progressive-death metall.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionPortable or not, variadic functions themselves are evil PinmemberPa3PyX12:25 14 Dec '11  
GeneralMy vote of 2 PinmemberMichael Imamura18:41 6 Apr '09  
General[Message Deleted] PinmemberIlya Parniuk2:04 19 Feb '08  
GeneralRe: Crash on memcpy Pinmemberaraud2:14 19 Feb '08  
Generalnot portable Pinmemberpeterchen6:20 29 Mar '05  
GeneralRe: not portable Pinmemberaraud18:55 29 Mar '05  
Look up first at top of this article. This marked as "VS Dev". Next point is that values must be pushed to stack "as is" not as reference because va_arg assumes that. And this would lead to broken logic if there was no distinction between variable passed by value and variable passed by pointer. If you know pointer, doesn't differs from reference for compiled code. So, the only thin place in this approach is that we can't get size of stack at compile time in order to make structure of proper size. But I assure you, call to function of such type like TRACE (it takes only digits and pointers) that overflows 50 DWORDS, looks like TRACE("try write in a sane format here", a,b,c,d,e,f,g...,x,y,z,a1,b1...,x1,y1,z1); If you write such code you can change 50 to 100 to satisfy needs. Access violation indeed can have place but only if memory right after stack is marked as NOACCESS. But I suppose, probability of such occasion is VERY low. Because stack must grow. And for this bad case we can use try{}catch{} around memcpy. This is what I'm going to do. Thank you for your criticism. Big Grin | :-D
 
"7 You shall have no other gods before me. 8 You shall not make an images in order to bow down to them or serve them. 11 You shall not take the name of the LORD your God in vain. 12 Observe the sabbath day 16 Honor your father and your mother, that your days may be prolonged. 17 You shall not kill. 18 Neither shall you commit adultery. 19 Neither shall you steal. 20 Neither shall you bear false witness against your neighbor. 21 Neither shall you covet anything that's your neighbor's." Your God
GeneralRe: not portable Pinmemberpeterchen19:18 29 Mar '05  
Generalportable Pinmemberaraud21:41 29 Mar '05  
GeneralRe: portable PinsussAnonymous2:19 22 Jun '05  
GeneralRe: portable PinmemberJan Richardson1:48 7 Aug '05  
GeneralRe: portable Pinmember__PPS__14:37 18 Apr '07  
GeneralRe: portable Pinmemberaraud18:52 18 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 29 Mar 2005
Article Copyright 2005 by araud
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid