Sprintf () garbled output

I have the following code:

char buf[255]; int ID = 0; double val = 1.234; sprintf(buf, "% d@ %.3lf", ID, val); printf("%s", buf); 

printf() correctly prints the ID value, but the val value is just garbage. Do you know what causes this problem? I am using MSVC 9 on Win7 32-bit. Even stranger, the above code works correctly on a 64-bit machine.

Thanks in advance for your help.

+4
source share
1 answer

after this comment:

Yes, there is an additional code. This code is compiled into a DLL, and in fact I have three places where I call the code sent in my question, and only in one place in the garbage output, as described.

This is probably due to some problems elsewhere in your code. You are likely to corrupt the stack / heap somewhere and inadvertently overwrite your variable. Test your code with a memory check (valgrind (unix), CMemLeak (all))

+2
source

Source: https://habr.com/ru/post/1436150/


All Articles