I have just encountered a rather non-trivial problem. First, look at this code:
char buffer; sprintf_s(buffer,24,"Year=%f",2011);This works beautifully! The string is copied nicely into the buffer as it is supposed to, and afterwards everything else continues smoothly.
Now, look at this code:
char buffer; sprintf_s(buffer,24,"Year=%f",2011);This is nothing less than a pure catastrophe! As soon as it is executed the program crashes, Windows pops up a message-box telling me that the program needs to close, and I am pretty sure that if it wasn't for Windows' Security Center it would erase my hard-drive as well.
The thing is that the first lines of code are executed in the main thread (the program) while the second ones are executed in a DLL procedure called by the main thread. Under different circumstances I would agree that two lines of code is not much to go on, but the essence of the problem can really be boiled down to why sprintf_s is causing the program to crash when invoked in a DLL procedure? (If you want to try it yourself first create a DLL file and export a function using sprintf_s. Then simply call the function from a test app to see the crash).
I have no idea what's causing this behavior so I am crossing my fingers one of you will be able to enlighten me.
PS1: I have also tried using std::stringstream/strings to solve the problem (converting a floating-point value to ASCII), but this solution crashed as well.
PS2: Floating-point support is loaded in both the program and in the DLL.