Thread: Core Dump with Variable Arguments

  1. #1
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47

    Core Dump with Variable Arguments

    Code:
    I have written the following code which accepts variable arguments:
    
    void dprintf(FILE *fp,char *fmt,...)
    {
            char OldMsg[BUFSIZ];
            va_list lArgs;
    
            if(fmt && *fmt)
            {
                fmt[strlen(fmt)-1]='\0'; /* Remove ending \n */
                va_start(lArgs, fmt);
                vsprintf(OldMsg, fmt, lArgs);
                va_end(lArgs);
                fprintf(fp, "%s at %s_%02d\n", 
                            OldMsg, Run.date, Run.hour);
            }
    }
    
    I am calling the function as follows:
    
    dprintf(flog,"\t%s(%s): Unable to insert node\n", 
                 program_name, sccs_ver);
    My program core dumps because of this routine. Can anyone tell me what's wrong with it? I have written similar functions in the past and never had an issue.

  2. #2
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    Thank you very much. I did realize that it throws away the last char unconditionally.

    The whole reason I created the routine was because their were a million fprintf's in the program and I needed a way to easily add a date/time stamp to the end of the messages without having to do a bunch of manual work. I verified ahead of time that all the previous calls ended in a \n and will always end that way. I was so focused on something being wrong with the var message part I overlooked the const pointer issue - so thank you all very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-08-2007, 11:47 AM
  2. core dump....
    By Shogun in forum C Programming
    Replies: 1
    Last Post: 06-03-2003, 08:41 AM
  3. core dump
    By lonbgeach in forum C Programming
    Replies: 1
    Last Post: 04-07-2003, 03:34 PM
  4. problem with strtok & core dump
    By razza in forum Linux Programming
    Replies: 4
    Last Post: 04-24-2002, 04:56 PM
  5. segmentation core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 04:43 PM