Re: Access violation at the end of the program
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Tue, 28 Aug 2007 11:53:10 -0500
"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:u0i8d39s5uuq4at5j0t0hji6khprank5ph@xxxxxxxxxx
On Tue, 28 Aug 2007 15:16:43 -0000, Hunter <Igal.Hunter@xxxxxxxxx> wrote:
// Global Vars
char SystemTimeChar[] = "yyyy_mm_dd-HH_mm_ss_ccc";
void main ()
{
char logfilename[] = "EV_yymmdd_hhmmssccc.txt";
SetFileName (logfilename);
void RetrieveDateTime(char *SystemTimeChar)
{ // return date and time format: yyyy_mm_dd-HH_mm_ss_ccc
sprintf(SystemTimeChar, "%04d_%02d_%02d-%02d_%02d_%02d_%03d",
SysTime.wYear, SysTime.wMonth,\
SysTime.wDay, SysTime.wHour, SysTime.wMinute, SysTime.wSecond,
SysTime.wMilliseconds);
void SetFileName(char *filename)
{
RetrieveDateTime(SystemTimeChar);
sprintf(filename, "EV-%s.%s", SystemTimeChar, EXT);
You have a buffer overrun in SetFileName's sprintf, which is overwriting
main's return address on the stack. I'd guess you got to this point by
changing the literal you use to initialize SystemTimeChar, but you forgot
to update the literal you use to initialize logfilename. You should always
try to avoid this sort of dependency; here, you can use string
concatenation:
#define TIME_FORMAT "yyyy_mm_dd-HH_mm_ss_ccc"
char SystemTimeChar[] = TIME_FORMAT;
char logfilename[] = "EV_" TIME_FORMAT ".txt";
Now you only need to update TIME_FORMAT. Of course, your filename
extension
had better not be longer than 3 characters.
A little extra buffer size, like _MAX_PATH + 1, surely wouldn't hurt,
because there's only one of these buffers. Trying to size the buffer as
small as possible is only worthwhile if you're making hundreds or thousands
of them.
In the future, to help diagnose problems like this, look into the
compiler's /RTCs option, and to help avoid problems like this, look into
the "safe" version of sprintf and friends. Just be sure you don't ignore
the return codes for the latter. When you're using the original versions
of
sprintf, strcpy, etc, there is no room for error.
P.S. None of the line continuation characters you've sprinkled throughout
your code are necessary. Also, it's "int main()", not "void main()".
--
Doug Harrison
Visual C++ MVP
.
- Follow-Ups:
- Re: Access violation at the end of the program
- From: Hunter
- Re: Access violation at the end of the program
- From: Larry Smith
- Re: Access violation at the end of the program
- References:
- Access violation at the end of the program
- From: Hunter
- Re: Access violation at the end of the program
- From: Hunter
- Re: Access violation at the end of the program
- From: Larry Smith
- Re: Access violation at the end of the program
- From: Hunter
- Re: Access violation at the end of the program
- From: Larry Smith
- Re: Access violation at the end of the program
- From: Hunter
- Re: Access violation at the end of the program
- From: Doug Harrison [MVP]
- Access violation at the end of the program
- Prev by Date: Re: vc or vc++??
- Next by Date: Re: fstream "write" faster than Windows "WriteFile"
- Previous by thread: Re: Access violation at the end of the program
- Next by thread: Re: Access violation at the end of the program
- Index(es):
Relevant Pages
|