Thread: How to tell a va_list object is empty?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    How to tell a va_list object is empty?

    For example, I have
    Code:
    void INFO(const char* fmt, ...){
      char buf[1024]
      va_list ap;
      vsnprintf(buf, 1024, fmt, ap);
      va_end(ap);
      return;
    }
    I want to pass in 2 types: INFO("str") and INFO("str, %d", int). For the 2 types, shall I tell if ap is empty?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There is no way to tell by looking at the va_list. When you see the "%d" you assume there is an argument.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by brewbuck View Post
    There is no way to tell by looking at the va_list. When you see the "%d" you assume there is an argument.
    If I call INFO("str"), will there be an error?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, because you don't initialize the va_list with va_start. Other than that, no.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Other than that, no.
    Well, that, and other than that missing semicolon.
    Code:
    void INFO(const char* fmt, ...){
      char buf[1024];
      va_list ap;
      va_start(ap, fmt);
      vsnprintf(buf, 1024, fmt, ap);
      va_end(ap);
      return;
    }
    That return is unnecessary as well. And consider using sizeof() rather than hard-coding 1024 into that vsnprintf() call.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM