I'm trying to use va_arg in a function that gets dynamically allocated (with new) float arrays. However, should I attempt to access these arrays, such as with
You define scalar to be of the "real" class or struct, if this is not a POD, your program will terminate at run-time. I refer to this thread: http://www.cplusplus.com/forum/beginner/28894/
As well, that shouldn't matter, I'm trying to solve the problem with vector, not scalar
Actually, I've come up with a small, compilable code demonstrating my problem. If anyone could please try to do it on their own compilers, that would be much appreciated.
So, from what I gather from that link, C++ doesn't allow variable-argument functions? That's silly.
And are you suggesting overrunning the buffer? In this particular case, I actually know that all float*'s sent will be of size 4, so could I simply overrun the buffer?
EDIT: As well, it should be mentioned that in that small code, not even scalar, which is a POD, is getting the correct value. It's simply being given a value of 0.
Fair enough. However, given how I'm using Windows, the problem remains. Any hints?
I'm unsure as to how to "remake" va_args, given how each item given is not bundled into a vector and handed to the called function, but is sent individually. Thus, each item is not necessarily next to the other in the memory, correct?
To make your own all you need is some pointers and the address of the first variable passed to your function. I'll give a quick example that does nothing but shows what I mean:
You could use L B's code, which is, a quite nifty approach actually. It will still not work for non-POD's, though, as they can have different sizes (and you would have to send their sizeof() to the funtion aswell). But since variadic arguments are barely ever used (they seemed quite neat to me at first, but later I found they weren't too great), you might aswell just send a container (vector, list, queue, deque, map) to the function. It's a much safer way.
Why can't you use non-PODs with my method? You just need to know the structure of it and then you can find the variable that tells the size and loop through that.
Because a char pointer can contain only one byte of data. Furthermore, doing direct casts like that is not always safe. Using stl containers is more type-safe and potentially a lot faster.
True, but you'd have to template it to make sure that it works with everything, with non POD's, a part of the "data" the pointer refers to is about their member functions, not just data member. Casting this can give strange results at times. As I said, STL containers are the way to go.