473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Interleaved variable argument processing

I was recently asked whether it is legal to start processing a variable
argument list a second time, /before/ you've finished with it the first
time. (I have no idea why the questioner might want to do this, I'm afraid.
Since he's normally fairly bright, I presume he had a good reason for
asking.)

The following code illustrates the point, I think (but don't use it as a
va_arg tutorial!, as it kinda assumes there are at least six args after the
named arg):

#include <stdio.h>
#include <stdarg.h>

int foo(int i, ...)
{
va_list ap = {0};
va_list aq = {0};
int sum = i;

va_start(ap, i);
sum += va_arg(ap, int);
sum += va_arg(ap, int);
va_start(aq, i);
sum += va_arg(ap, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
sum += va_arg(aq, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
va_end(ap);
sum += va_arg(aq, int);
sum += va_arg(aq, int);
va_end(aq);
return sum;
}

int main(void)
{
printf("%d\n", foo(6, 5, 4, 3, 2, 1, 0));
return 0;
}

Having read through the relevant section of C99, I could find no
standard-based reason why this interleaving should not work (and, indeed,
it does work on the single compiler I tested the code with).

Did I miss something, or am I right in thinking this is a legal,
well-defined technique?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #1
10 2956
There is this to note:

[draft 99]

I7.15.1.4

clause 3.

"...va_start shall not be invoked again for the same ap [ va_list type ]
without an intervening invocation of va_end for the same ap."

Given common compiler implementations it's not surprising that it works
though.
Nov 13 '05 #2
Jason wrote:
There is this to note:

[draft 99]

I7.15.1.4

clause 3.

"...va_start shall not be invoked again for the same ap [ va_list type ]
without an intervening invocation of va_end for the same ap."

Given common compiler implementations it's not surprising that it works
though.


It's a different ap.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #3
"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:

<snip>

And that's the only suggestion I would have regarding your sample
code: instead of calling va_start twice, why not use va_copy ?


Perhaps because he doesn't have a C99 compiler. :-)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #4
In <bh**********@hercules.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:

<snip>

And that's the only suggestion I would have regarding your sample
code: instead of calling va_start twice, why not use va_copy ?


Perhaps because he doesn't have a C99 compiler. :-)


A brilliant illustration of my point: far too many people no longer have
a clear idea about what is a C feature and what is a C99 feature.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bh**********@sunnews.cern.ch...
| In <bh**********@hercules.btinternet.com>
| Richard Heathfield <do******@address.co.uk.invalid> writes:
|
| >"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
| >
| ><snip>
| >>
| >> And that's the only suggestion I would have regarding your sample
| >> code: instead of calling va_start twice, why not use va_copy ?
| >
| >Perhaps because he doesn't have a C99 compiler. :-)
|
| A brilliant illustration of my point: far too many people no longer have
| a clear idea about what is a C feature and what is a C99 feature.

NB: Richard mentioned the C99 standard in his original post,
so this is why I based my reply on this.

This said, I agree with your point.

IMHO, C99 and C90 deserve separate NGs nearly as much as C90 and C++98 do
... Maybe, maybe not ;)
Kind regards,
Ivan
--
http://www.post1.com/~ivec
Nov 13 '05 #6
In <3f********@news.swissonline.ch> "Ivan Vecerina" <iv**@myrealbox.com> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:bh**********@sunnews.cern.ch...
| In <bh**********@hercules.btinternet.com>
| Richard Heathfield <do******@address.co.uk.invalid> writes:
|
| >"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
| >
| ><snip>
| >>
| >> And that's the only suggestion I would have regarding your sample
| >> code: instead of calling va_start twice, why not use va_copy ?
| >
| >Perhaps because he doesn't have a C99 compiler. :-)
|
| A brilliant illustration of my point: far too many people no longer have
| a clear idea about what is a C feature and what is a C99 feature.

NB: Richard mentioned the C99 standard in his original post,
so this is why I based my reply on this.
Another (related) point of mine: people read the C99 standard and
expect their C89 implementations to behave accordingly. Which is sheer
foolishness.
This said, I agree with your point.

IMHO, C99 and C90 deserve separate NGs nearly as much as C90 and C++98 do
... Maybe, maybe not ;)


C99 already has a group. It's called comp.std.c. As long as
practically nobody writes C99 code (using the C99 extensions supported
by one C89 implementation or another doesn't count), there is little need
for a newsgroup dedicated to C99 programming.

Once people start using C99 implementations on a significant basis, c.l.c
will naturally "migrate" towards C99, the same way it happened with c.s.c
once the focus of the standardisation effort has moved from C89 to C9x.

The current foolishness in comp.lang.c is practically due to the fact that
an electronic copy of C99 can be cheaply obtained (and its last public
draft is still available for free), which was never the case for C89/C90
(whose electronic copy was both expensive and of poor quality: it
contained a raw scan of standard's pages).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
In <bh**********@titan.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
Dan Pop wrote:
Another (related) point of mine: people read the C99 standard and
expect their C89 implementations to behave accordingly. Which is sheer
foolishness.


No, it's relatively low-gradient foolishness. C99 has done very little to
change the parts of C that are already defined by C90. Some, sure, but not
much.


It doesn't matter how much: the text you're actually reading might be one
of those few places where C99 changes the C89 specification. Do you like
gambling on programming issues?

Furthermore, it is quite common for C89 implementations to provide C99
features as extensions, but with different semantics. E.g. snprintf on
Solaris or inline and VLAs on gcc.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8
Dan Pop wrote:
In <bh**********@titan.btinternet.com> Richard Heathfield
<do******@address.co.uk.invalid> writes:
Dan Pop wrote:
Another (related) point of mine: people read the C99 standard and
expect their C89 implementations to behave accordingly. Which is sheer
foolishness.
No, it's relatively low-gradient foolishness. C99 has done very little to
change the parts of C that are already defined by C90. Some, sure, but not
much.


It doesn't matter how much: the text you're actually reading might be one
of those few places where C99 changes the C89 specification. Do you like
gambling on programming issues?


No. I'd far rather use the C89 standard when writing C89 code.
Unfortunately, I don't have a copy of the C89 standard, although I do have
a draft, of course. Given the choice between gambling on a draft, and
gambling on an authoritative standard text that has been revised, I'll take
the latter, and combine it with the excellent group knowledge of
comp.lang.c in an attempt to arrive at a low-risk strategy. That, in fact,
was precisely what I was trying to do here.
Furthermore, it is quite common for C89 implementations to provide C99
features as extensions, but with different semantics. E.g. snprintf on
Solaris or inline and VLAs on gcc.


What about interleaved variable argument processing? Does the published C89
standard have anything to say on the matter?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #9
In <bh**********@hercules.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
Dan Pop wrote:
In <bh**********@titan.btinternet.com> Richard Heathfield
<do******@address.co.uk.invalid> writes:
Dan Pop wrote:

Another (related) point of mine: people read the C99 standard and
expect their C89 implementations to behave accordingly. Which is sheer
foolishness.

No, it's relatively low-gradient foolishness. C99 has done very little to
change the parts of C that are already defined by C90. Some, sure, but not
much.


It doesn't matter how much: the text you're actually reading might be one
of those few places where C99 changes the C89 specification. Do you like
gambling on programming issues?


No. I'd far rather use the C89 standard when writing C89 code.
Unfortunately, I don't have a copy of the C89 standard, although I do have
a draft, of course. Given the choice between gambling on a draft, and
gambling on an authoritative standard text that has been revised, I'll take
the latter, and combine it with the excellent group knowledge of
comp.lang.c in an attempt to arrive at a low-risk strategy. That, in fact,
was precisely what I was trying to do here.


Bad choice. The C89 draft is much closer to C89 than C99.
Unsurprisingly. And most of the differences between the draft and C89
are obvious to the competent C programmer (CLK_TCK instead of
CLOCKS_PER_SEC, the wrong argument type for %o and %x in printf formats).
Furthermore, it is quite common for C89 implementations to provide C99
features as extensions, but with different semantics. E.g. snprintf on
Solaris or inline and VLAs on gcc.


What about interleaved variable argument processing? Does the published C89
standard have anything to say on the matter?


Any good reason to expect the text to be different from the draft?
Having access to both, I can confirm that the <stdarg.h> specification
is the same in both documents.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10
Dan Pop wrote:

<snip>
What about interleaved variable argument processing? Does the published
C89 standard have anything to say on the matter?

<snip>
Having access to both, I can confirm that the <stdarg.h> specification
is the same in both documents.


There! That wasn't so hard, was it? Thanks for the confirmation.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #11

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

Similar topics

0
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
23
by: Daniel Rudy | last post by:
Hello, I'm trying to learn how command line arguments are handled in C. The following code segment that I wrote as a test program compiles, but when I try to run it, it core dumps. This is...
0
by: Bill | last post by:
I a C# developer getting involved in an image processing application. I am receiving a stream of bytes from a SQL Server database that contains image data. The data can be streamed in one of the...
3
by: universalbitmapper | last post by:
Hi, This exercise (From PAM525Web Development) asks me to display a table of reviews, then sort the reviews depending on which column heading is clicked. I can't find how to write this line:...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
34
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements....
2
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application....
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.