473,395 Members | 1,462 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,395 software developers and data experts.

va_copy implementation

What would be a good implementation of va_copy where va_list is a typedef
for char *?

Can I jyust copy the pointer's value or is it impossible to tell without
knowing more details?
Nov 13 '05 #1
3 9072
In article <news:bp**********@news1.tilbu1.nb.home.nl>
Servé Lau <i@bleat.nospam.com> writes:
What would be a good implementation of va_copy where va_list is a typedef
for char *?

Can I jyust copy the pointer's value or is it impossible to tell without
knowing more details?


I think the word "impossible" is too strong, because there are only
a few "typical" implementations for variadic functions in C. Given
the claim that "va_list" is just "char *", it is *likely* that
simply copying the value suffices. It is not guaranteed, though.

The C99 standard requires that va_copy be implemented as a macro,
so if your goal is to achieve va_copy on both C89 and C99 systems,
you can resort to something like:

#ifndef va_copy

/* WARNING - DANGER - ASSUMES TYPICAL STACK MACHINE */
#define va_copy(dst, src) ((void)((dst) = (src)))

#endif

You can dress up the "#define" in further #ifdefs to check for
known machine types (e.g., the usual powerpc va_copy is more
complicated), or you can just require that anyone porting the
software provide a "machdep/stdarg.h" header or some such, that
uses the existing C99 <stdarg.h> if it exists, or else adds the
C99 va_copy() macro atop the existing C89 <stdarg.h>.

Also, you could write a test program that "experiments with" any
va_copy you define, to see if a simple "copy the value bits" approach
works. If you pass integers (int and long and, in C99, long long;
char and short widen), floating-point values (double and long
double; float widens), and several different types of pointers,
and can access all of those correctly, you are probably close.
Add struct and union objects of varying sizes and you will test
the usual remaining special cases (and you may even discover that
the system-supplied <stdarg.h> breaks when va_arg is supposed to
extract a structure -- not that I know of particular systems where
this occurs; it just seems likely).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #2
"Chris Torek" <no****@torek.net> wrote in message
news:bp**********@elf.torek.net...
The C99 standard requires that va_copy be implemented as a macro,
Thanks for answering. FYI I did find taht va_copy and va_end do not have to
be macro's

7.15.1 from C99
The va_start and va_arg macros described in this subclause shall be
implemented

as macros, not functions. It is unspecified whether va_copy and va_end are
macros or

identifiers declared with external linkage.

so if your goal is to achieve va_copy on both C89 and C99 systems,
you can resort to something like:

#ifndef va_copy

/* WARNING - DANGER - ASSUMES TYPICAL STACK MACHINE */
#define va_copy(dst, src) ((void)((dst) = (src)))

#endif

You can dress up the "#define" in further #ifdefs to check for
known machine types (e.g., the usual powerpc va_copy is more
complicated), or you can just require that anyone porting the
software provide a "machdep/stdarg.h" header or some such, that
uses the existing C99 <stdarg.h> if it exists, or else adds the
C99 va_copy() macro atop the existing C89 <stdarg.h>.

Also, you could write a test program that "experiments with" any
va_copy you define, to see if a simple "copy the value bits" approach
works. If you pass integers (int and long and, in C99, long long;
char and short widen), floating-point values (double and long
double; float widens), and several different types of pointers,
and can access all of those correctly, you are probably close.
Add struct and union objects of varying sizes and you will test
the usual remaining special cases (and you may even discover that
the system-supplied <stdarg.h> breaks when va_arg is supposed to
extract a structure -- not that I know of particular systems where
this occurs; it just seems likely).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to

spammers.
Nov 13 '05 #3
>"Chris Torek" <no****@torek.net> wrote in message
news:bp**********@elf.torek.net...
The C99 standard requires that va_copy be implemented as a macro,

In article <news:bq**********@news2.tilbu1.nb.home.nl>
Servé Lau <i@bleat.nospam.com> writes:Thanks for answering. FYI I did find taht va_copy and va_end do not have to
be macro's

7.15.1 from C99
The va_start and va_arg macros described in this subclause shall be
implemented as macros, not functions. It is unspecified whether va_copy
and va_end are macros or identifiers declared with external linkage.


Ah -- sorry about that, I should have noted that I am still using
a C99 draft. Mine says:

7.12.1 Variable argument list access macros

[#1] The va_start, va_arg, and va_copy macros described in
this subclause shall be implemented as macros, not
functions. It is unspecified whether va_end is a macro or
an identifier declared with external linkage.

In this case -- unlike that for stdin/stdout/stderr, for instance
-- #ifdef-test-ability for va_copy might have been fairly useful
(as it can be used to test for implementations that have some C99
features, including va_copy, but are not yet C99-conforming and
hence do not define __STDC__ appropriately).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #4

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

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
29
by: Enrico `Trippo' Porreca | last post by:
Both K&R book and Steve Summit's tutorial define a getline() function correctly testing the return value of getchar() against EOF. I know that getchar() returns EOF or the character value cast to...
4
by: Johan Lindh | last post by:
Hi all, If I read the standard correctly, a va_list variable that's initialized using va_copy() should be terminated with a va_end() on it. Is this correct? int test( va_list x ) { va_list...
1
by: Ian Pilcher | last post by:
Can va_copy be used to copy a va_list object that was initialized in a different function? (I.e. the function in which va_copy is used is called by the function in which the original va_list is...
4
by: REH | last post by:
Is there a standard when to implement va_copy in the pre-99 standard? Is this allowed by the standard: #define va_copy(DEST, SRC) memcpy((DEST), (SRC), sizeof(va_list)) Thanks.
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.