Corrupted stack problem


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?

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);
   return lbuff;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
tBuffer is probably too small, try increasing the size
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,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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).
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

No problem, seems I was some second faster ...
AndyAinscowFreelance programmer / ConsultantCommented:
Now why didn't I see that ?  duh!
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 ;)
OcranaAuthor Commented:
Great, thanks,
>> 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.