Sprintf() doesn't work in release mode

Started by
7 comments, last by JohnBolton 17 years, 8 months ago
Hi, I'm trying to figure out why the following code works in debug mode, but not in release mode:

// Given m_Game_ID_Nums[0] == 1, m_Game_ID_Nums[1] == 7
// m_Game_ID_Nums[2] == 4, m_Game_ID_Nums[3] == 0
// Note:  m_Game_ID_Nums is an integer array

char temp_buffer[8];
sprintf(temp_buffer, "%d%d%d%d", m_Game_ID_Nums[0],
	m_Game_ID_Nums[1], m_Game_ID_Nums[2], m_Game_ID_Nums[3]);

temp_buffer, in debug mode, will read "1740", as expected, but in release mode, it reads "1070400". The only way to get the desired result is through the following code

// Given m_Game_ID_Nums[0] == 1, m_Game_ID_Nums[1] == 7
// m_Game_ID_Nums[2] == 4, m_Game_ID_Nums[3] == 0
// Note:  m_Game_ID_Nums is an integer array

char temp_buffer[8];

for (int i = 0; i < 4; i++)
{
	_itoa(m_Game_ID_Nums, &temp_buffer, 10);
}

temp_buffer[4] = '\0';

In this case, temp_buffer always reads "1740". Why doesn't sprintf() work properly in release mode? Is there some way I can make it work, no matter what the mode? Or do I have to wiggle around it with _itoa()? It makes the code easier to write and understand, I believe, when using straight-up sprintf(). -Gauvir_Mucca
Advertisement
humm wierd indeed.
Try the following :

char temp_buffer[8];
sprintf(temp_buffer, "%i%i%i%i", m_Game_ID_Nums[0],
m_Game_ID_Nums[1], m_Game_ID_Nums[2], m_Game_ID_Nums[3]);

I dont think it makes a difference, but me I'm always using %i for int and it work fine. Just test it in case :)
#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv){	int m_Game_ID_Nums[] = {1, 7, 4, 0};	char temp_buffer[8];	sprintf(temp_buffer, "%d%d%d%d", m_Game_ID_Nums[0],		m_Game_ID_Nums[1], m_Game_ID_Nums[2], m_Game_ID_Nums[3]);	printf("%s\n", temp_buffer);	return 0;}


worked fine in release on Visual C++ .Net 2003
I tried your suggestion, Daivuk, and I still have the same problem.

I'm using MSVC 2005 Express Edition.

It's frustrating that it only occurs with the release version, so I can't trace the program and try and step through it.
Frustrating weird errors like this tend to be the result of stack smashing (i.e., somewhere in your program you are messing up your program's stack majorly) often by misusing *printf and *scanf functions, somewhere else in the code.

If you must use sprintf, use snprintf instead. It will help avoid stack problems.
It's Unlikley the bug is in sprintf(). You wanna post more code???
I have had a similar problem with VS 2005 Express. My problem stranger still since it disappeared when I removed a line that wasn’t supposed to be read by the program. The error disappeared later on and I never looked further into it. The only advice I can give you is to try and clean both the debug and release directories and do a complete rebuild. Not just rebuild but clean and the build. Also try and remove the ncb-database file and rebuild again.

Best of luck to you
dont use the *sprintf() functions, they are rather unreliable and problematic in themselves.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Perhaps it has something to do with Unicode settings, but buffer overrun (as etothex suggested) is more likely.

BTW, you can step through the code in a Release configuration, but since the code has been optimized, it doesn't work as well as stepping through the code in a Debug configuration.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement