Passing on variable number of arguments...again

Started by
10 comments, last by Fruny 18 years, 3 months ago
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
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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
Quote:Original post by Thunder Sky
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?).



Since you're already using C++, you could use boost::format.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Since 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.
Quote:
Since you're already using C++


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.
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
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.
Quote:Original post by Thunder Sky
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.


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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement