Unity Passing on variable number of arguments...again

This topic is 4608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I know a simular problem has already been discussed here but those answers only aplied when passing to libraryfunctions. What if I want to pass on the arguments to one of my own functions? Heres what I meen:

//This one formats the output and returnes the string
char* MyFormatFunction(char *input, ...) {
va_list valist;
va_start(valist,input);

int length=512; //should be enough, havn't come up with a better solution
char* str=(char*) malloc(length);
vsnprintf(str,512,input,valist);
va_end(valist);
return str;
};

char* MyPreProcessorFunction(char* input, ...) {
//
//I do some stuff with the input string here
//

char* str=MyFormatFunction(input, ...) // <- how do I pass on the arguments?
return str;
};


I know I could make MyFormatFunction take a va_list as the second argument but then I wouldnt be able to call it directly in ordinary printf style. You might also wonder why on earth I would declare a new function like MyFormatFunction when what it does basicly can be sumerized in just two lines of code (malloc and vsnprintf). To tell you the truth I cant remember right now :P. But I know it was necessery. One reason I can think of now is that I wanted to wrap all my output functions with a nice prefix and I wanted MyFormatFunction to be a part of that group. Thanks in advance for any replies. /ThunderSky

Share on other sites
You need to break MyFormatFunction into two functions, just like printf/vprintf and similar library functions. Then, have the variadic version call the one that takes a va_list parameter.

Hard-coded buffer sizes = evil.

Share on other sites
Returning new'd string buffers like that is IMO evil as well, unless you have a clear understanding of who owns the string and therefor who is responsible for cleaning it up, or if you return a smart pointer to the buffer.

Share on other sites
Quote:
 You need to break MyFormatFunction into two functions, just like printf/vprintf and similar library functions. Then, have the variadic version call the one that takes a va_list parameter.

Thanks Fruny, quite simple, this shows that I still have a lot to learn...

Quote:
 Hard-coded buffer sizes = evil.

I dont like that "should be enough", and whats even wors is that I have no error checking yet either. But as I understand it the implementations of sprintf and related functions seldom comply with the standard so I cant really be sure if will return the buffersize needed if an error occurs.

I read that there is a specific function that doesnt acctually return the formatted buffer, but the size that would be needed to store it. Unfortunatly, it seems to be IDE dependent. I got hold of the VC version on microsofts developer pages but Im using DevC++ (MinGW) so that didnt help much. I later found that there is a g++ version (MinGW is a g++ compiler right?). But when I tried to compile whith the g++ version DevC++ said it was undeclared so...Im cinda lost. If you know how to solv the problem, great! otherwise I think Ill just leav it. Not verry nice, but it'l be enough for what Im using it for.

Quote:
 Returning new'd string buffers like that is IMO evil as well, unless you have a clear understanding of who owns the string and therefor who is responsible for cleaning it up, or if you return a smart pointer to the buffer.

yeah I know..guess Im just an evil cinda guy...(whats a smart pointer?)

Edit. Forgot the thank you guys for the comments so..thanks

Share on other sites
Quote:
 Original post by Thunder SkyI got hold of the VC version on microsofts developer pages but Im using DevC++ (MinGW) so that didnt help much. I later found that there is a g++ version (MinGW is a g++ compiler right?).

Since you're already using C++, you could use boost::format.

Share on other sites
Quote:
 Original post by FrunySince you're already using C++, you could use boost::format.

Seconded. boost::format is nice and powerful and type safe and all that.

A smart pointer is a class that basically deletes an object when the last reference to it is destroyed. In your code above you return a malloc'd char buffer. If you returned

boost::shared_array<char> MyFunction(){boost::shared_array<char> mysmartbuffer(new char[length]);}// do stuffreturn mysmartbuffer;

This would return a 'smart pointer' to this memory block. When the last smart pointer that references that block goes out of scope or is otherwise delted, the buffer will be deleted automatically, resulting in no memory leak and no need to worry about if the block gets deleted properly or not.

Share on other sites
Quote:

Well I guess I do, but I have no idea (well some idea) of how to use classes and a lot of the other C++ stuff. Havnt got the time or strength to read up on the subject... so I tend to stick to the more basic C. for now.

besides, Im using this for a small chat program so I prefer not to use any nonstandard libraries.

Share on other sites
Which brings me to another thing Ive been meaning to ask about. If I include a file, say string.h, how much does the final executable grow in size. would it be better to do a
for(i=0;buffer!='\0';i++);
instead of including the whole string.h file and using strlength?

Mainly Im asking because windows.h is huge when you count in all the other files it inculdes and I like to use Sleep() in my simpler console apps and I read somewhere that that could be acheaved some other way.

thanks for the other replies

Share on other sites
Smart compilers don't leave un-used code in the final executable. The actual code for the functions you don't use should not end up in the .exe. It may increase compile time a bit though, but not enough to worry about whether you should use external stuff.

Share on other sites
Quote:
 Original post by Thunder SkyWell I guess I do, but I have no idea (well some idea) of how to use classes and a lot of the other C++ stuff.

To a large extent, using existing classes should be easier than writing classes. Especially with well-designed libraries.

Quote:
 Havnt got the time or strength to read up on the subject... so I tend to stick to the more basic C. for now.

Quote:
 Thanks Fruny, quite simple, this shows that I still have a lot to learn...

Do you want to learn or not? [smile] The best way to learn is to actually start using the libraries. You'll probably quickly make up for the "lost" time.

Quote:
 besides, Im using this for a small chat program so I prefer not to use any nonstandard libraries.

The C standard library does not include any networking facilities, so you're going to need non-standard libraries anyway. Additionally, quite a few components from the Boost library have been accepted into the upcoming revision of the C++ standard library. You're on shaky grounds to use non-standardization as an argument for rejection. [smile]

Quote:
 and I like to use Sleep() in my simpler console apps

Sleep() isn't a standard C function either. [wink]

Quote:
 (whats a smart pointer?)

A smart pointer is an object designed to act like a pointer syntactically, but with additional functionality, such as keeping track of the number of references to the block it points to, and automatically releasing the block when that reference count falls to zero.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 20
• 11
• 13
• 14
• 78
• Forum Statistics

• Total Topics
632142
• Total Posts
3004393

×