views:

122

answers:

2

Hi,

I used sprintf method to format data to a string which I want to write to a file, in C++ console application using VS 2008. The Input is a particular message, which has various variables and values (ex: Type 'int' and Value '10' / Type string and value "abc", etc.) When I send a two messages it works perfectly. But When I send more than two messages it gives a runtime error saying 0xC0000005: Access violation reading location 0xabababab. Why is this happening? Is it because the method 'sprintf' has a default buffer length? How can I overcome this problem?

+1  A: 

No, there is no default size. It will assume that there is space enough, and write as much as specified by the formatting string.

Your access violation says reading, which hints that there might be something wrong with your arguments, perhaps they don't match the formatting string.

You need to have exactly as many arguments, of the proper types and in the correct order, as you reference using %-codes in the formatting string.

unwind
Hi, thanks a lot for the help. I'll recheck it.
Isuru
+1  A: 

By the way, you might want to have a look at this page.

0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory

Markus