unknown characters in buffer

I have a problem at following action in my software:

  1. define 2 buffers… auto char buffer [64]
  2. put characters into buffer1
  3. put other characters and the characters from buffer1 into buffer 2 with the function strcat

And sometimes i get the problem, that at the end of buffer2 are unkwnon characters.
The only solution is to reduce all buffer from [64] mybe [32].

normal buffer:
q9xxxxxH09d%E01604023C431B97%E01604023C4349C8%E01604023C433DCD, 29

buffer with unkown characters:
q9xxxxxH09d%E01604023C431B97%E01604023C4349C8%-ß/Þæ-ßóÝDÆñø"ßóÝ#@ÞäøŠ#±ß#pÇà

Is there a limit? Are there any methods to check the free ram memory?

Without seeing your code it is hard to say, but the probable cause is lack of nul termination on the buffers. strcat and similar functions rely on a nul or 0 byte to indicate the end of the string(s) being worked on.

Regards,
Peter

Hi Peter, thank you for the answer

Copying the code ist not easy, because the problem happens on a complex operation.

What happens if the 0 byte appaers more than 1 times in the string? Is it possible to 0-terminate the string more that one times?

I terminate a string by strcat(buffer, “\0”) is that correct?

Hi,

To terminate a buffer you need to know where the buffer should end and add a null there so for example if I had copied 10 characters into buffer2 at I could terminate it by adding:

buffer2[10] = 0;

or

buffer2[10] = ‘\0’;

Another approach to take would be to fill the destination buffer with 0s before copying in the data so that the remainder of the buffer will always be nuls e.g.

memset(buffer2, 0, 64);

using strcat(buffer, “\0”) will not work for 2 reasons as strcat first scans the string to find the end of it which is marked by a null so you don’t know where it will end up secondly strcat(buffer, “\0”) adds a 0 length string to the end of a nul terminated string which means it does nothing!

Regards,
Peter