C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-20-2003, 09:28 AM   #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.
penney is offline   Reply With Quote
Old 06-20-2003, 10:11 AM   #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.
penney is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Compiler is trying to convert identical variable arguments .c and .h files Phanixis Linux Programming 4 10-08-2007 11:47 AM
core dump.... Shogun C Programming 1 06-03-2003 08:41 AM
core dump lonbgeach C Programming 1 04-07-2003 03:34 PM
problem with strtok & core dump razza Linux Programming 4 04-24-2002 04:56 PM
segmentation core dump - need help knight101 C++ Programming 1 11-26-2001 04:43 PM


All times are GMT -6. The time now is 01:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22