should I call free() in a loop to clean an array address?

this function, in a loop, prepares an array of bytes to be shown on a display.
I noticed that when the displayed text changed to a smaller text, some random characters were being displayed in the end of new text. I think this is because of the remaining bytes from previous displayed characters which were not overwritten by new text.

void showChars(byte *b){
  uint8_t len = b[0]-1;
  char toChar[128];
  for (uint8_t i = 0; i < len ; i++) {
    toChar[i] =  b[i+1] ;
  }
  pdisplay.drawBodyText( toChar );  //this function clears the display before inserting new text.

  //free(toChar) //is it safe?

}

I temporarily solved this issue by adding a condition to overwrite characters with ' ' if position is greater than the length of text

for (uint8_t i = 0; i < 128 ; i++) {
    if(i < len){
      toChar[i] =  b[i+1] ;
    }else{
      toChar[i] = ' ';
    }
  }

but I wonder if I could free memory of char toChar[] using free() or if any solution.

Your array toChar[128] is an uninitialized local array and as such, the contents of it are undefined. If you simply change your declaration to include initialization, char toChar[128] = {};, the array will be seeded with NULLs, problem gone.

Oh, I was not aware of that empty initialization! Thank you so much!

bontempos:
//free(toChar) //is it safe?

No.

The question has already been answered. But, in general (AFAIK) you should only free() memory that you malloc(). But, even at that , I've heard (but never experienced) that excessive malloc-ing / free-ing will cause you grief in a memory-constrained AVR processor. You probably have more leeway in a 32-bit ESP8266 or ARM-based processor with more than 10x the RAM.

But, in general (AFAIK) you should only free() memory that you malloc().

That is correct.

If you malloc() (or calloc() or realloc()) memory, you should use free() to free it.

If you use new() to allocate memory, you should use delete() to free it.

If you do not use either function to allocate memory, do NOT use free() or delete().

malloc and new allocate an object or array on the heap, and such things never go away unless you make them
with the appropriate free/delete call.

Local variables are created for you on the stack, and always go away when you exit the function they are
local too.

If you keep hold of a reference or pointer to a local object or array and pass it out of the function somehow, it
will be a "dangling pointer", bad news.