Go Down

Topic: Problems with va_list wrapper.. (Read 798 times) previous topic - next topic

jake1981

Hi.

I have been working with a serial protocol lately.. Looking good.
I have a function that formats my data to suite the protocol. It accepts variadic parameters (uint8_t argCount, ... ). Works perfectly.

Okay, I also need to write some helpers- as my protocol has feature that data must be verified with answer.
So, for most data sent over the protocol, it would be very nice to have a function that would send the data, wait for answer and then return answer as return value, right?
Just to make writing my program faster and easier, and keeping the code not so long, this helper would be perfect, right?
Well, to my original function that accepts variadic parameters.. I guess this won't work very well (actually it works.. but yes, not that well):
Code: [Select]


int other_function(int numArgs, ...) {
return 0;
}

int my_function(int numArgs, ... ) {
va_list ap;
va_start(ap, numArgs);
other_function(numArgs, ap);
va_end(ap);
return 0;
}


At least, on my Mac, where I have use gcc 4.2-
This won't do good..

This one on the other hand, does work..
Code: [Select]

// int other_function(int numArgs, va_list ap = NULL); // this is important if it's inside a class, va_list variable has to have a default value

int other_function(int numArgs, va_list ap ) {
int b;
for ( b = 0; b < numArgs; b++ ) {
char *s = va_arg(ap, char*);
printf("%s ", s);
}
return 0;
}

int my_function(int numArgs, ... ) {
va_list ap;
va_start(ap, numArgs);
other_function(numArgs, ap);
va_end(ap);
}

int main() {
my_function(3, "First", "Second", "Third");
printf("\r\n");
return 0;
}


Okay, it's not a copy-paste from my test code or anything similar, I wrote it from my head so it might have some small problems why it won't compile straigth away.. But yes.. That does work..
Similar code has been tested.. And used as well..
---

So, I took it to Arduino as well.. I am using Arduino Due, so I have mac Arduino app v 1.5.something.. Equipped with gcc 4.4..

And with that I get a strange warning:
Quote

note: the mangling of 'va_list' has changed in GCC 4.4
error: default argument for parameter of type 'va_list' has type 'int'


See my remark there, that if I want to use code like this in a Class.. I need to initialize va_list variable to something.. And null seems to fit it perfectly..
Well, this error message stops compiling and.. Makes me think.. NULL should be 0.. But maybe it won't work that way, I'll change default value from NULL to 0..
Damn, error won't go away..

I have googled this and found out that Android developers have been around this as well a lot. They haven't found out how to fix this, except, with this compiler CFLAG: -Wno-psabi

Where can I put this, so it effects my code?

---

Second, how to make this thing work without using the compiler flag, as I really would be more interested in tweaking my own code, than changing Arduino environment..

jake1981

Actually.. I was able to make a workaround..

i added va_list valist; to my class..

then I initialize ... to the valist of class before proceeding to my wrapper.. A bit ugly, but works.

---

But if one has solution to original problem- I'd be interested..

Go Up