0

Problem Partically solved: please read bottom

The variadic function in question, stripped down to the bare simplest is:

inline Variant::Variant(int type, int dims, ...) { va_list ap; va_start (ap, dims); //Removed Variant handling code here int i=0; for(;i<dims;i++){ int size = va_arg (ap, int); } //Likewise, Removed Variant handling code here va_end (ap); } 

As you can see, the function in question is an overloaded constructor for my Variant class. This particular constructor takes in the size and number of dimensions when declaring an array.

So this line runs in my main():

Variant(ARRAY_ARRAYTYPE, 3, 800, 22, 22); 

and when I breakpoint right after the

int size = va_arg (ap, int); 

line in the function, I get the following results:

size holds the value of 800 (correct) size holds the value of -43587879... (WTF) size holds the value of 4387643543 (WTF) 

Then the program segfaults because obviously those are completely invalid values.

Any ideas? Thanks in advance.

Problem Partically solved: restart somehow fixed that problem but now a new one ...

My OTHER variadic function, which is called afterwards, seems to pickup whatever parameters were passed to the Variant() constructor rather than its own ones. Any ideas?

Variant* ArAcc(Variant* in_variable, ...) { va_list app; int index=0; int i, j; int mult; va_start (app, in_variable->arrayaccess->num_dimensions); for(i=0;i<in_variable->arrayaccess->num_dimensions;i++){ mult = 1; for(j=i+1;j<in_variable->arrayaccess->num_dimensions;j++){ mult = mult * in_variable->arrayaccess->dim_sizes[j]; } int size = va_arg (app, int); mult = mult * size; index += mult; } va_end (app); return &((*in_variable)[index]); } 

with...

ArAcc(&SomeVariant, 7, 9); 

Thanks in advance.

10
  • Have you tried adding __cdecl modifier? Commented May 2, 2012 at 10:57
  • yep, nothing changes, same problems. Commented May 2, 2012 at 11:00
  • I tried it with g++ 4.6.2 (printing size in the loop): it works. Commented May 2, 2012 at 11:08
  • Thats really strange. Well im compiling on windows with both VC++2008 and minGW and they both have this problem. Ill try a restart. Commented May 2, 2012 at 11:12
  • 3
    linux.about.com/library/cmd/blcmdl3_va_start.htm states that va_start should have the last known parameter of the function, so it should be "va_start(app, in_variable);", not "va_start (app, in_variable->arrayaccess->num_dimensions);", which is strange because you said that this version works... Commented May 2, 2012 at 11:25

2 Answers 2

2

Edit: I didn't see that this was already mentioned in a comment.

If a restart solved the problem, that would indicate to me that the problem isn't really solved, just happens not to cause symptoms.

If the first problem really is solved, the following line in the second function may be causing the second problem:

va_start (app, in_variable->arrayaccess->num_dimensions); 

That should be

va_start (app, in_variable); 

of course.

Based on your comment I suspect that when the first case wasn't working, you were actually passing a local variable as the second parameter to va_start instead of the function argument.

Sign up to request clarification or add additional context in comments.

1 Comment

@64bit_twitchyliquid I am not stefaanv who gave the solution in a comment. If he gives it as an answer you may want to accept that instead.
0

try removing inline and make it a normal function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.