Serial Input - RX Buffer

I am working on a project that needs communication between arduino and python, and found this Serial Input Basic post to be very helpful. However, there is a section of the code that I don't understand why it was done the way it is. The previous post is locked, so I am starting this new topic.

In example 2 & 3 of the Serial Input Basic, the following code was used while saving the received data in receivedChars array:

if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }

// Where const byte numChars = 32;

Question 1: Why would one need the part below? :

if (ndx >= numChars) {
ndx = numChars - 1;
}

I found a few articles pointing that it is related to 32-byte RX buffer, but I still don't understand what that means. Wouldn't this if clause just mean replacing the last element in the array (that is exceeding 32 chars) with the new incoming character? i.e. that 32nd char is lost?

Question 2: Can't you just declare "receivedChars" to be a string, then add new characters after one another (concatenation)? What are the limitations when comparing a string and array to record serial input?

Much appreciated.

techtana:
Question 1: Why would one need the part below? :

Why would one write outside of an array?

techtana:
Question 2: Can't you just declare "receivedChars" to be a string, then add new characters after one another (concatenation)?

If you want to have a useless variant, go on.
Strings are an No-No on 8 bit Arduinos with very low RAM.

So, that mean the transmitted data must always be within 32 characters. Otherwise, the rest of the data will just be lost?

How can I determine how long the string or array can be for a given RAM? This sounds like a foundation of computing, but I just started learning programming, so I'm still piecing things together.

techtana:
So, that mean the transmitted data must always be within 32 characters. Otherwise, the rest of the data will just be lost?

Yes, when messages come in that are longer than your buffer, you have to deal with them.
Dropping chars at the end of the line seems sensible to me.

If you have to receive longer lines, make the buffer bigger.

Thank you

techtana:
So, that mean the transmitted data must always be within 32 characters. Otherwise, the rest of the data will just be lost?

You can change the value in the const byte numChars = 32 to a bigger number. But whatever number you choose you must be careful not to write past the end of the array.

What is the longest single message that you want to be able to receive?

...R

Keep in mind also that this buffer is not the actual Rx buffer, the actual Rx buffer (64 bytes, but can also be increased) needs to be read by the program before it overflows (or bytes will get lost). Keep this in mind when writing tasks that are performed between calling the 'non-blocking' Read function.

Deva_Rishi:
Keep in mind also that this buffer is not the actual Rx buffer, the actual Rx buffer (64 bytes, but can also be increased) needs to be read by the program before it overflows (or bytes will get lost).

Good point.

That is why I asked the OP what is the longest message s/he needs to receive.

...R

Thank you. I thought about this again and realized I can just send a few smaller sections of messages that is less than 32 characters / bytes. So, this should work out now. Thanks again, this really clears up my confusion about the purpose of the code!!