Berkeley CSUA MOTD:Entry 28060
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/07/08 [General] UID:1000 Activity:popular
7/8     

2003/4/10 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:28060 Activity:high
4/9     Right now I have tons of snippets like
        sprintf(errmsg, "Error %d: invalid type %d at %d\n", 1, myType, time);
        QueueError(errmsg);
        Is it possible to define a macro to take an arbitrary # of arguments--
        #define ERROR(x) sprintf(globErrMsg, x); QueueError(globErrMsg);
        Or will the x in the macro just take everything up to the first comma?
        \_ #define ERR(args...) {sprintf(g_errMsg, args);QueueError(g_errMsg);}
        \_ Yes, if you use recent gcc or other C99-ish compilers, no for
           older ones.
        \_ A reliable method for doing this is to write a function that
           uses a va_list and vsnprintf:

           #include <stdarg.h>
           void Error (char *err, size_t sz, const char *fmt, ...) {
                va_list vl;
                if (err == null || sz <= 0)
                  return;
                memset(err,'\0',sz);
                va_start(vl,fmt);
                vsnprintf(err,sz,fmt,vl);
                va_end(vl);
                QueueError(err);
           }

           BTW, You should really avoid sprintf since it doesn't have a
           good way to check thesize of the input buffer.
           \_ thanks for the code, I'll give that a try today.  One question--
              this is a real-time application, so there are a few common stdio
              calls I use regularly and the rest are viewed warily.  Are there
              any non-constant performance implications for using vsnprintf?
              I can handle a +foo hit, but foo*strlen(err) might be pushing it.
              thanks.
           \_ that only matters if the content of your variables comes from
              input.
           \_ thanks for the code, I'll give that a try today.  One
              question-- this is a real-time application, so there are
              a few common stdio calls I use regularly and the rest
              are viewed warily.  Are there any non-constant
              performance implications for using vsnprintf? I can
              handle a +foo hit, but foo*strlen(err) might be pushing
              it. thanks.
                \_ vsnprintf calls vfprintf internally. In most cases
                   sprintf also calls vfprintf internally. The same
                   non-constant performance issues you may have with
                   your implementation of sprintf will most likely
                   apply to your implementation of vsnprintf.
           \_ that only matters if the content of your variables comes
              from input.
                \_ Program defensively.
2025/07/08 [General] UID:1000 Activity:popular
7/8     

You may also be interested in these entries...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2010/6/4-30 [Computer/SW/Languages/C_Cplusplus] UID:53849 Activity:nil
6/4     Is this valid C++ code?
        std::string getStr(void) {
            std::string str("foo");
            return str;
        }
        void foo(char *s);
	...
2008/4/15-23 [Computer/Companies/Google] UID:49755 Activity:high
4/15    Anyone got any random investment tips? I am kinda at a loss of what to do.
        Agriculture and energy are at record highs...
        \_ stockhouse.ca .. NOT - via NOSOF ... assay results coming in soon
        \_ I'm liking HRP, currently paying 12.2% dividend; there's some
           risk, but they shouldn't be as exposed to some of the macro
           issues as a lot of stocks.  Ex-div is coming up next week.
	...