473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: parsing variable arg lists via va_list pointers (any gurus here?)

On Jun 3, 3:23*am, Jesse Ziser <d...@spam.diew rote:
>*ANSI says that when you pass a va_list object to
a function and call va_arg() on it within that function, the value of
the va_list object is undefined upon return.

The relevant paragraph from the Standard is:

---- Begin Quote ----
The type declared is va_list
which is an object type suitable for holding information needed by the
macros
va_start, va_arg, va_end, and va_copy. If access to the varying
arguments is
desired, the called function shall declare an object (generally
referred to as ap in this
subclause) having type va_list. The object ap may be passed as an
argument to
another function; if that function invokes the va_arg macro with
parameter ap, the
value of ap in the calling function is indeterminate and shall be
passed to the va_end
macro prior to any further reference to ap.
---- End Quote ----

At first reading, I thought this was just saying the following:
* C passes arguments by value, i.e. the object in the calling
function remains unchanged
* The va_* macros alter their va_list argument
* You can only use the most up-to-date va_list, you can't use an
old out-of-date one

*Behold:

void parse_half_of_a rg_list( va_list args )
{
* *...
* *foo_t x = va_arg( args, foo_t );
* *bar_t y = va_arg( args, bar_t );
* *...

}

void superfunc( int a, ... )
{
* *va_list args;

* *va_start( args, a );

* *parse_half_of_ arg_list( args );
* */* Uh-oh! *ANSI says args is undefined now,
* * * but there's still more to parse */
* *parse_half_of_ arg_list( args );

* *va_end( args );

}

The va_list object in "main" remains unchanged, therefore it is no
longer "up to date" when you go to try use another va_* macro on it.

So, I reasoned, why not pass a va_list * instead of a va_list? *Surely
that can't do any harm... I mean, since va_list is a data type, it
should be possible to create a pointer to it, and *(&x) should be the
same as x for any named type and for any operations, right? *Lo:

void parse_half_of_a rg_list( va_list *args )
{
* *...
* *foo_t x = va_arg( *args, foo_t );
* *bar_t y = va_arg( *args, bar_t );
* *...

}

void superfunc( int a, ... )
{
* *va_list args;

* *va_start( args, a );

* *parse_half_of_ arg_list( &args );
* *parse_half_of_ arg_list( &args );

* *va_end( args );

}

I haven't been able to find anything in any standard that explicitly
allows or prohibits this, but there aren't many ways I can think of that
some devious library author could screw this up. *The thing is, this
code will exist for a long time and will quite possibly need to run
under every semi-major system and C compiler that will come into
existence in the next 20 years. *As of right now it will need to
immediately work on Sun, Mac, Cygwin, and Linux and compile under gcc,
pgcc, and icc.

I find the original paragraph from the Standard to be very strange. I
don't see why it went to the bother of mentioning calling and called
functions when it just could have said "the va_list must always be up-
to-date". This kind of leads me to believe that maybe there's some
other requirement than the va_list being up to date... maybe something
to do with the stack.

So, I guess the gist is that I want to do something really weird but I
want it to be really portable, and I'm afraid that might be an
impossibility. *Not being able to do this would mean I'd have to use a
bunch of structs or arrays with void pointers everywhere, which is a
considerable burden as this function will be called very frequently.

I am well aware that this is a really bizarre thing to have to do.

Please let me know if you know a system that breaks this, or if you have
good cause to believe that this will always work.

Thanks in advance.

Wait and see what others have to say.
Jun 27 '08 #1
5 4636
On Jun 3, 4:28 am, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
I find the original paragraph from the Standard to be very strange. I
don't see why it went to the bother of mentioning calling and called
functions when it just could have said "the va_list must always be up-
to-date". This kind of leads me to believe that maybe there's some
other requirement than the va_list being up to date... maybe something
to do with the stack.
There are some interesting architectures out there that do interesting
things in the va_arg macros. Sparc machines come to mind, or Itanium.
There may be compiler support needed (even the x86-64 ABI looks like a
major pain).

Also note that va_list could be defined either as an array or as a
struct. All the macros will work if you use them as the C Standard
says, but the effect of applying an address operator or having a
pointer to a va_list could be very different in both cases.
Jun 27 '08 #2
christian.bau wrote:
On Jun 3, 4:28 am, Tomás Ó hÉilidhe <t...@lavabit.c omwrote:
>I find the original paragraph from the Standard to be very strange. I
don't see why it went to the bother of mentioning calling and called
functions when it just could have said "the va_list must always be up-
to-date". This kind of leads me to believe that maybe there's some
other requirement than the va_list being up to date... maybe something
to do with the stack.

There are some interesting architectures out there that do interesting
things in the va_arg macros. Sparc machines come to mind, or Itanium.
There may be compiler support needed (even the x86-64 ABI looks like a
major pain).

Also note that va_list could be defined either as an array or as a
struct. All the macros will work if you use them as the C Standard
says, but the effect of applying an address operator or having a
pointer to a va_list could be very different in both cases.
My problem is precisely that the C standard _doesn't_ really say how to
use them. It only outlines the basics.

Could you explain how address operators and pointers could behave
differently depending on whether va_list is a struct or array type? To
my knowledge, if stdarg.h contains either of these:

typedef weird_t va_list[42];
typedef struct weird va_list;

And I declare:

va_list args;

Then, in either case, *(&args) will still behave exactly the same as
args. Is there some case I'm not thinking of in which this is not true?
Jun 27 '08 #3
Jesse Ziser said:

<snip>
>
Could you explain how address operators and pointers could behave
differently depending on whether va_list is a struct or array type? To
my knowledge, if stdarg.h contains either of these:

typedef weird_t va_list[42];
typedef struct weird va_list;

And I declare:

va_list args;

Then, in either case, *(&args) will still behave exactly the same as
args. Is there some case I'm not thinking of in which this is not true?
No. * and & "cancel".

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #4

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:xM******** *************@b t.com...
Jesse Ziser said:

<snip>
>>
Could you explain how address operators and pointers could behave
differently depending on whether va_list is a struct or array type? To
my knowledge, if stdarg.h contains either of these:

typedef weird_t va_list[42];
typedef struct weird va_list;

And I declare:

va_list args;

Then, in either case, *(&args) will still behave exactly the same as
args. Is there some case I'm not thinking of in which this is not true?

No. * and & "cancel".
So in other words, *(&args) is the same as args like he said?

(There is a slight difference: args has to be an lvalue for *(&args) to
work, so *(&1234) and 1234 are not the same since the former won't
compile -- assuming a suitable context.)

--
Bartc
Jun 27 '08 #5
Bartc said:
>
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:xM******** *************@b t.com...
>Jesse Ziser said:
<snip>
>>Then, in either case, *(&args) will still behave exactly the same as
args. Is there some case I'm not thinking of in which this is not
true?

No. * and & "cancel".

So in other words, *(&args) is the same as args like he said?
Yes. My "no" was in response to "is there some case I'm not thinking of in
which this is not true".
(There is a slight difference: args has to be an lvalue for *(&args) to
work, so *(&1234) and 1234 are not the same since the former won't
compile -- assuming a suitable context.)
Oops, you're right - I missed that case. Thanks for the correction.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8313
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double AFunct2 ( int num, ... ); double average ( int num, ... )
3
4730
by: Anders Andersen | last post by:
Hi, I have a question about about the use og va_start, va_arg, va_end, ... I'm trying to pass a variable argument list from one function to another, but I can't get it to work. My code looks someting like this: int func_one(char *arg, ...) { va_list ap;
3
2783
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For example, if A is a wrapper function around B). Thanks, Nimmi
6
2304
by: giulianodammando | last post by:
In the development of a simple numerical simulation software i need to read some initialization parameters from a file that looks like: # Global Setup species = 1; \begin{specie}<1> name = NITROGEN;
1
2337
by: braj | last post by:
Hi Friends, I was trying a test code to use the icu stuff with variable arguments and I seem to be heading nowhere. Can anyone please help. Given below is the test code and the Output. Thanks a ton in advance -braj
9
2577
by: Chad | last post by:
This might be a bit vague and poorly worded..... In my program, I handle function failures using fprintf() and exit() like: fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); There are 5 of these. Since each one has two lines, the total lines of
13
4475
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
11
11045
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1. printf("Hey! how are you"); /*No. of arguments = 1*/ 2. printf("Sum of %d and %d is %d",a,b,c"); /* No. of arguments = 4*/
0
227
by: Peter Nilsson | last post by:
On Jun 4, 1:20 am, Amandil <mazwo...@gmail.comwrote: That violates a constraint on implementations where va_list is an array. -- Peter
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.