Operating System - HP-UX
1832872 Members
2566 Online
110048 Solutions
New Discussion

Re: building va_list variable in HP-UX Integrity

 
rakeshcs123
Advisor

building va_list varialbe in hpux itanium

Hi Guys,
I want to copy value in the va_list variable.
I use to do this in HP-UX PARISC as follows

va_list fun(void **vals, int numvals)
{
int x;
out=(void**)malloc(sizeof(void*)*(numvals+1));
for (x=0;x {
out[numvals - 1 - x] = vals[x];
}
out[x] = (void *)numvals;
return (va_list)(out + numvals);
}

But typecasting of void** to va_list is not working in HP-UX itanium.
can you please help me to know how can i build a va_list variable with some arguments.

Thanks in advance
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: building va_list variable in HP-UX Integrity

What you are doing isn't portable. The only supported way to get a va_list is with va_start.
It does appear you know that the parms are in reverse order. But not that it is off by one.

Also, you need to allocate an array of long long, not void*. Unless you know they are all pointers??
As to your return, you need to use a type hammer to get this right. va_list is a thingy and you can't do much with it. See:
http://h30499.www3.hp.com/t5/Languages-and-Scripting/experiencing-different-behaviour-for-va-list-initialization-with/m-p/5094394

out += numvals + 1
return *(va_list*)&out;

rakeshcs123
Advisor

Re: building va_list varialbe in hpux itanium

Thanks for the reply. But this does not solve my problem.
My problem is i want to send variable arguments to a function.
I have two interface
1) method(char *val, ...)
{
va_list arglist;
va_start(arglist, val);
method(val, arglist);
va_end(arglist);
}
2) method(char *val, va_list arglist)
{
//Process the arglist
}


When i know how many arguments i have to pass to function i use the first function method call
if i dont know the number of arguments to pass then i first create the va_list variable with all the arguments and then pass it to second function method.

i used above method fun to create the va_list variable in other platforms, but in hpux itanium this does not work. can any body tell me how to create va_list in hpux itanium so that i can pass those variables to second function.

Many thanks in advance...
Dennis Handly
Acclaimed Contributor

Re: building va_list variable in HP-UX Integrity

>My problem is i want to send variable arguments to a function. I have two interface
>1) method(const char *val, ...)
> method(val, arglist);

This is Standard and portable.

>2) method(const char *val, va_list arglist)

Again Standard and portable, provided your va_list is ONLY created by method 1).

>i first create the va_list variable with all the arguments and then pass it to second function method.

This is not portable and so is non-supported and illegal. Only va_start can create a va_list.

Note: you didn't see this here but you might try something like this. :-)

It is PA32 with the reversed list. IPF and PA64 have args in increasing order.
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
// portable solution
void foo(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    vprintf(fmt, ap);
    va_end(ap);
}
// not portable at all!
void bar(const char *fmt, void **vals) {
    int numvals, i;
    for (numvals = 0; vals[numvals]; ++numvals) {}
    long long *out = (long long*)malloc(sizeof(long long)*numvals);
    for (i = 0; i < numvals; ++i) // widen to register size
       out[i] = (long long)vals[i];
    // use a type hammer

   vprintf(fmt, *(va_list*)&out);
}
int main() {
    foo("%s %s %s\n", "hi", "ho", "off we go");
    const char *lists[] = { "hi", "ho", "off we go", NULL };
    bar("%s %s %s\n", (void**)lists);
}

Dennis Handly
Acclaimed Contributor

Re: building va_list variable in HP-UX Integrity

>for (i = 0; i < numvals; ++i) // reverse

Oops, this is the wrong comment. It really should be: // widen to register size

Dennis Handly
Acclaimed Contributor

Re: building va_list variable in HP-UX Integrity

If the replies answered your questions, you should read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33