Corrupted stack problem

Hi,

I used following code in my Unicode app, but I got everytime a message that "The Stack around tBuffer is corrupted". I do not see any flaw there. Any tips?

Ocrana
time_t rawtime;
   struct tm * timeinfo;
   TCHAR tBuffer[10];
   long lbuff;
   time ( &rawtime );
   timeinfo = localtime( &rawtime );
   _tcsftime(tBuffer, sizeof(tBuffer), _T("%Y%m%d"), timeinfo);
   lbuff=_tstol(tBuffer);
   return lbuff;

Open in new window

OcranaAsked:
Who is Participating?
 
ZoppoCommented:
Hi Ocrana,

I guess the problem is that the buffer size passed as second paramter should be treated as number of characters, not as size in bytes.

If you pass it like this in my test app the error got vanished:

> _tcsftime(tBuffer, sizeof(tBuffer) / sizeof(TCHAR), _T("%Y%m%d"), timeinfo);

Hope that helps,

ZOPPO
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
tBuffer is probably too small, try increasing the size
0
 
evilrixSenior Software Engineer (Avast)Commented:
This line

   _tcsftime(tBuffer, sizeof(tBuffer), _T("%Y%m%d"), timeinfo);

Needs to be this

   _tcsftime(tBuffer, sizeof(tBuffer)/sizeof(TCHAR), _T("%Y%m%d"), timeinfo);

Otherwise you are telling the function that the size of the buffer is 20 and not 10 characters long (assuming TCHAR is 2 bytes in the case of a wide build).
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
evilrixSenior Software Engineer (Avast)Commented:
Oops, sorry Zoppo :)
0
 
ZoppoCommented:
:o)

No problem, seems I was some second faster ...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Now why didn't I see that ?  duh!
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Now why didn't I see that ?  duh!
If it's any consolation I had to read the code about 10 times before I spotted it ;)
0
 
OcranaAuthor Commented:
Great, thanks,
0
 
ZoppoCommented:
>> Now why didn't I see that ?  duh!
I didn't really see it before - and I found (at least in MSDN) the description of the second parameter isn't well phrased:

>  maxsize
> Size of strDest buffer.

I would interpret this as 'size in bytes' which would imply the original code is correct. Butm, on the other hand I saw that '10' has to be large enough since the result could at maximum be 8 characters long.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.