LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Variable Arguments: How to identify number of passed arguments

I'm using the macros (va_start, va_arg, va_end) in a function which takes a variable number of arguments.
Currently I have to pass the amount of expected arguments or an special value (e.g. -1) to identify the last argument.
I'm looking for a possibility to get the number of passed arguments automatically.
How can CVI identify, that e.g. sprintf() got to many arguments or not enough arguments???
0 Kudos
Message 1 of 4
(3,253 Views)
It looks at the format specifiers (e.g. %d, %f, %s...) in the format string to figure out how far it has to index through the stack to access each individual parameter. For instance printf("%d %f", myint, myfloat); knows that since the first specifier was %d that the first argument is 32-bits wide and is an integer, and it will increment ptr_arg (common name for the argument pointer) 4 bytes to access the float that comes after it in the stack. There is usually a required parameter like this in a function that has variable argument lists so it knows how to access parameters correctly and doesn't corrupt the stack. Although you could implement it in many different ways.

An available explanation that I know of on the web for this concept is at http://msdn.microso
ft.com/library, just search for va_arg and you will find a variable argument list overview document.

Jason F.
Applications Engineer
National Instruments
www.ni.com/ask
0 Kudos
Message 2 of 4
(3,253 Views)
I know, that it is possible to get the amount of expected parameters from the format string.
But just in case, that there are more specifiers in the format string than passed arguments, you usually get a strange output, because the function takes the next address in the memory an interpretes it according to the specifier.
But not in CVI. There you'll get an error, that the amount of passed arguments does not fit to the amount of specifier.
I think the only way to find out, how many arguments are passed to the function is use a predefined macro of the preprocessor. e.g.__NUM_OF_ARGUMENTS__
Are there such (hidden) macros avaiable in CVI?
0 Kudos
Message 3 of 4
(3,253 Views)
No, there are no hidden macros in CVI, if there were then they would be for internal use only and would not be exposed to you at all.

You are only going to get these library error messages when running your app with the debugger. If you run a release executable with an improper number of arguments as compared to the format specifiers you will get the same behavior you described in a CVI app as well. If you want to turn off the library errors, you can go to the Options >> Run options... menu selection and in this dialog uncheck the "break on library errors" box. To protect against improper use of your functions all that you can really do is come up with a clever scheme (like the format string) to insure that your library end-user inputs parame
ters into your functions correctly.

Jason F.
Applications Engineer
National Instruments
www.ni.com/ask
0 Kudos
Message 4 of 4
(3,253 Views)