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

Using variadic functions within variadic functions

Can I use printf inside a variadic function that I am writing?

Like:

void print(const char * format,...)
{
va_list va;
va_start(va, format);
char buffer[64*1024]; // yeah, I know...
vsprintf(buffer, format, va);
va_end(va);
printf("--- %s ---", buffer);
}

I know the example is degenerate, but is it allowed, that is, does it
have welldefined behavior?

I would imagine that printf itself would also have need for the
va_list, va_start etc. mechanism.

/David

Feb 17 '06 #1
2 2067
pi************@gmail.com wrote:
Can I use printf inside a variadic function that I am writing?

Like:

void print(const char * format,...)
{
va_list va;
va_start(va, format);
char buffer[64*1024]; // yeah, I know...
vsprintf(buffer, format, va);
va_end(va);
printf("--- %s ---", buffer);
}

I know the example is degenerate, but is it allowed, that is, does it
have welldefined behavior?


If 'buffer' contains a string, printf("--- %s ---", buffer);
is ok, and legal. No magic tricks here, and this has nothing to
do with varargs.
Feb 17 '06 #2
On 17 Feb 2006 01:45:37 -0800, "pi************@gmail.com"
<pi************@gmail.com> wrote:
Can I use printf inside a variadic function that I am writing?
Yes. As long as you declare it correctly, by #include'ing <stdio.h>,
as you must for _any_ call to it.
Like:

void print(const char * format,...)
{
va_list va;
va_start(va, format);
char buffer[64*1024]; // yeah, I know...
vsprintf(buffer, format, va);
va_end(va);
printf("--- %s ---", buffer);
}

I know the example is degenerate, but is it allowed, that is, does it
have welldefined behavior?
As long as a similar direct call to s[n]printf would, namely: you
don't overflow buffer, a risk you already alluded to, and could
protect against in C99 by using vsnprintf; and the data arguments you
receive and pass on using va correctly match the format specifiers.
(And don't overlap, but nothing validly provided by the caller can
overlap your 'auto' buffer.) And you don't overflow the stack.
I would imagine that printf itself would also have need for the
va_list, va_start etc. mechanism.

No problem; varargs works separately for each vararg function. You can
have vararg call vararg, vararg call nonvararg, vararg call nonvararg
that calls vararg, etc., etc. The only thing you can't do is pass your
own varargs on to another routine _as varargs_; instead you pass the
entire list (or a suffix of it) as a va_list -- as you correctly do.

The other thing to be careful of is that you can't pass 'your' va_list
to a function that uses it to access the arguments (like vs[n]printf
usually) _and_ use that same va_list to access the arguments yourself,
because it is unspecified whether va_list is an array 'by reference'
type or not. In C99 you can use va_copy to handle this. Or, even in
C89, you can start, use and end two va_list objects explicitly; or one
object with distinct (non-overlapping) ranges.

Oh, and to be super-picky: your 64*1024 is nominally done in 'int'
arithmetic, which is allowed to be only 1+15 bits and could overflow.
In practice this will always be done at compile time and should be at
least diagnosed, but is not strictly required to be. Moreover, 65536
chars is greater than the minimum object size that must be supported,
even (very slightly) in C99, and exceeding a resource limit isn't
required to be diagnosed, although compile-time cases should be.

And finally, it is implementation-dependent whether the last line of a
text stream (which stdout is) must end in a newline. Your function by
itself is valid, but if used in a program that does not subsequently
output a newline on such a system it won't work as desired.

- David.Thompson1 at worldnet.att.net
Feb 27 '06 #3

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

Similar topics

19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Florian G. Pflug | last post by:
Hi I faintly remember that I once stumbled upon a way to declare variadic functions (functions that take a variable number of arguments) in plpgsql - but I just searched the docs and can't find...
5
by: goldfita | last post by:
I'm not sure if I'm doing something stupid, or if I'm not understanding how to use variadic functions. These functions appear to be working individually. In ast_call, I can vprintf the argument...
2
by: baranikumarhtsl | last post by:
Hi all, Its again me only.... no only i am learning variadic functions topic and as soon as i completed i thought to write a example function . but i got stuck up!.... See i wants to...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
8
by: keith | last post by:
Someone please help me out here - I'm having a 'bad brain' day. I have a number of C logging functions, prototyped as, say: extern "C" { mylog1(char *arg1, char *arg2, char *arg3);...
2
by: Boltar | last post by:
Hi Is it possible to next variadic functions. Eg if I wish to do something like this... void mainfunc(char *fmt, ...) {
4
by: Boltar | last post by:
Hi Is it possible to nest variadic functions? Eg to do something like this: void mainfunc(char *fmt, ...) { va_list args; va_start(args,fmt);
4
by: Boltar | last post by:
Hi I think I posted a question similar to this on here a while back but I can't find the thread and I need something more general anyway. My problem is I have 2 variadic functions , one of...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.