Sign in to follow this  
Helderash

The purpose of stdarg.h?

Recommended Posts

Hello folks! After spending a good deal of time reading "C++: A Beginner's Guide" by Herbert Schildt, and doing a couple of C/C++ courses at university I thought I'd try my hand at "Programming Role Playing Games with DirectX 2ed." by Jim Adams. I was just wondering if anyone can help me out a little on a piece of his source code. In his section on Building an Application Framework, he includes a function to display error messages to the user in case an error occurs. A section of this function reads: va_list valist; if(Fatal == FALSE) strcpy(CaptionText, "Error"); else strcpy(CaptionText, "Fatal Error"); va_start(valist, Text); vsprintf(ErrorText, Text, valist); va_end(valist); I'm guessing va_list, va_start, va_end, etc. are part of stdarg.h, though I've never come across them before. I was wondering if anyone could let me know what these do, and why they are necessary. Thankyou for everything! James.

Share this post


Link to post
Share on other sites
They're used for passing a Variable number of Arguments (hence the VA prefix) to a function. An example is printf(), where you can pass as many parameters as you like (Well, so long as you have enough stack space).

A use for them is in a logging function (Like the one the author uses). So you can pass a format string (E.g. "Error %d: %s\n") and some parameters (E.g. 42, "It's broken") to it.

Share this post


Link to post
Share on other sites
They are used to create functions that take variable parameters (like printf). This is a C idiom (called the elipsis construct). It is not really recommended for C++ code because in C++, you can achieve the same functionality, in a type safe way, with function overloading and streams.

Share this post


Link to post
Share on other sites
Ah, thankyou very much. I'll leave them in for now, and when I've seen the function used a little later in the book I'll attempt to modify them to make use of the C++ features.

No doubt I'll find a million and one things to trouble me before then though :P

Share this post


Link to post
Share on other sites
There's lots of ugly stuff in there, really. You shouldn't be using any of the str* family of functions in C++; it's a different language from C, and it provides a standard library with *real* strings in it. ;) Also, C++ provides a *real* boolean type, called 'bool' in *lowercase*, and its values are 'true' and 'false', also lowercase. Also, there's no good reason to compare things to literal true and false.

Converting a va_args function to use streams instead is not trivial, and can't be done without seeing all the context. The thing is that the *interface* will change (very much for the better). Chances are that you want to make your own *object* that represents an "error stream", which holds on to a string-stream as a "buffer" for error text to display, and exposes a stream-like interface (overloads of the operator<< for example) and a "display" function (you could actually overload the operator<< to respond to std::endl by displaying a message box containing the .str().c_str() of the stringstream buffer).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this