noise on char array

EDITED,
To explain my self more clearly -
The following code:

    byte arrLength = 5;
    char testArray[arrLength];
    for(byte j=0; j<arrLength;j++){testArray[j] = 'a';}
    Serial.print(":");Serial.print(testArray);Serial.println(":");

when applied on a new sketch - gives the correct output - :aaaaa:
when applied on a old, multi tub, long sketch - gives the wrong output - :aaaaa⸮zgetUid:

The lines I have tired putting in the code,
(one at a time, once before, and once after, the loop)
in order to “clear” the array before print, are:

  1. while(Serial.available() > 0) {Serial.read();}
  2. free(testArray);
  3. testArray[0]= ‘\0’;
  4. memset(testArray, 0, sizeof(testArray));
  5. memset(testArray, 0, (sizeof(testArray)/sizeof(testArray[0])));

I have also tried renaming the array, changing its length to more than 10,
serial printing before and after in a loop.

nothing helps.

RazVanMal:
This simple code gives 2 different results.
On an empty sketch, it works fine. (output :aaaaa:)
on a large sketch, I get noise on the array. (output :aaaaa⸮zgetUid:)

I have tried:
while(Serial.available() > 0) {Serial.read();}
free(testArray);
testArray[0]= ‘\0’;
memset(testArray, 0, sizeof(testArray));
memset(testArray, 0, (sizeof(testArray)/sizeof(testArray[0])));

No result.

The code:

    byte arrLength = 5;

char testArray[arrLength];
    for(byte j=0; j<arrLength;j++){testArray[j] = ‘a’;}
    Serial.print(":");Serial.print(testArray);Serial.println(":");

Both of your code are not correct.
Code 1:
Assuming “testArray” is an array, call to free(testArray) will invoke undefined behavior.
Otherwise, if free(testArray) was successful, then the following statement would crash your program.

Code 2: A char array must be null-terminated, otherwise it will also invoke undefined behavior.

Dear arduino_new, hi.

I've edited the question as to explain the situation better.

I don't have 2 codes.
only 1 code.
Which i have tried in multiple variations.

In order to save us all the trouble of pasting over 20 different variations of the same 3-line-code,
I instead thought it was easier to just give the original code, and the different lines that
I've tried. I case it was not clear, they are not to be taken literally, as a standalone code.

Thank you for your observation.

arduino_new:
Code 2: A char array must be null-terminated, otherwise it will also invoke undefined behavior.

For print to work properly, a char array must have a null character at the end of the text. The array must me large enough to hold the text plus the null character. The null character is used to indicate the end of the text, if it is not present, print will continue to send whatever random data is in memory until it encounters a null, which generally is displayed as random garbage in the serial monitor.