Hello,
I am having a problem with what looks like an uninitialized call stack on Solaris 5.5.1 while
using a variable argument list function. An example program is at the end of this message.
The program was compiled with g++.
Incorrectly, both finalArray1 and finalArray2 receive the string "Hello". When I debugged it,
I found the second invocation of bundle_args was picking up the stack variables
from the previous call.
When I compiled and ran this on an hpux10 machine, it worked correctly.
Anyone seen this before? If so, any work-arounds? (I can pass "0" as a third parameter, but
this is kind of kludgy).
Thanks,
Pat Griffin
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char **
bundle_args(char *header, int iter, ...)
{
va_list listPtr = NULL;
int i = 0;
int num = 0;
char **charArray = NULL;
char **arrayPtr = NULL;
char *arg = NULL;
va_start(listPtr, iter);
num = va_arg(listPtr, int);
if (num)
{
charArray = (char **)calloc(num + 1, sizeof(char *));
arrayPtr = charArray;
printf ("%s, iteration %d\n", header, iter);
for (i = 0; i < num; i++)
{
arg = va_arg(listPtr, char *);
*arrayPtr = (char *)malloc(strlen(arg) + 1);
strcpy(*arrayPtr, arg);
arrayPtr++;
}
*arrayPtr = NULL;
}
va_end(listPtr);
return(charArray);
main()Quote:}
{
char name1[] = "Hello";
char **finalArray = NULL;
char **finalArray2 = NULL;
char **finalPtr = NULL;
finalArray = bundle_args("We are in bundle_args", 1, 1,
name1);
finalArray2 = bundle_args("We are in bundle_args", 2);
finalPtr = finalArray;
while (*finalPtr != NULL)
printf("%s ", *finalPtr++);
printf("\n");
finalPtr = finalArray2;
while (*finalPtr != NULL)
printf("%s ", *finalPtr++);
printf("\n");
Quote:}