sprintf causing weird serial issues?

Hi all,

I'm working on a project that I need to do some formatting to a string, I was having some weird bugs so I decided to start isolating parts of my project to find out what might be causing the issue.

Part of my project uses the sprintf function to format a char array for me. I know the arduino does not support floating points numbers by default with sprintf, currently I am just working with integers and char[] right now.

The issue I have found is that the serial output as soon as I start using sprintf goes all crazy! It'll work fine before, but as soon as I even call on the function even if I am not printing the data the serial output goes crazy!

My only thought was that maybe the serial library uses sprintf? I've posted the code that shows what happens.

This code prints gibberish to the serial monitor

void setup() {

	Serial.begin(19200);

}

void loop() {

	int tempReset = 5263;
	char sinceReset[] = "";
	sprintf(sinceReset, "SINCE RESET", tempReset);
	Serial.println("Hello");
}

This code works just fine!

void setup() {

	Serial.begin(19200);

}

void loop() {

	int tempReset = 5263;
	char sinceReset[] = "";
	//sprintf(sinceReset, "SINCE RESET", tempReset);
	Serial.println("Hello");
}

Thanks in advance

Writing to a zero length string is not the cleverest thing you could do.
(Or should that be " getting sprintf to write to a zero length string "? Whatever)

AWOL:
Writing to a zero length string is not the cleverest thing you could do.
(Or should that be " getting sprintf to write to a zero length string "? Whatever)

What would be the correct way? Create a string with a set length that I know is within the range of my final string?

Could that be why I am having the serial issues?

Use a fixed length buffer at least the size of the biggest text plus one.

Yes, the memory corruption probably generates your problem.

I miss a format specifier for tempReset and the code that prints the generated string.

char sinceReset[63];
sprintf (....)
Serial.print(sinceReset)

Don't use more than 63 because that is the default size of the tx buffer in the serial library. You can make it smaller to your needs (remember space for thr terminating nul character as said above). You also might want to consider snprintf so you will never get an overflow.

Awesome, thanks for you all your help I got everything working now. Now I know not to initialize a zero length char[]...I suppose I would need to call malloc() if I were to want to change a zero length char[] no?

For this project I won't need to go over 63 characters, but its good to know that I can use snprintf if I need to go larger.

Thanks again!

isaucy:
For this project I won't need to go over 63 characters, but its good to know that I can use snprintf if I need to go larger.

snprintf only protects the memory, it does not allow to print more than 63 chars at a time without blocking.

Please show your current code.

isaucy:
For this project I won't need to go over 63 characters, but its good to know that I can use snprintf if I need to go larger.

That will not work; snprintf will just throw everything away that exceeds the the size of the character array (minus one).

isaucy:
Awesome, thanks for you all your help I got everything working now. Now I know not to initialize a zero length char[]...I suppose I would need to call malloc() if I were to want to change a zero length char[] no?

No, you will just declare a pointer and use that in combination with malloc.