Ah, I have an idea what might be happening here: I believe that valgrind is actually pretty smart.

If you have a buffer of size 128, and gethostname() only fills in, say, the first 32 bytes (including the \0), the other 128-32=96 bytes are uninitialized. You can MPI_Allgather these, in which case those 96 uninitialized bytes will be copied over to the hostname_recv_buf buffer.

For each rank, valgrind can actually track the local memcpy from local_hostname to hostnam_recv_buf[rank * MAX_LEN_SIZE], and it knows that those 96 bytes are still uninitialized.

So when you go to strcmp them later, valgrind says "ah ha! those are uninitialized!"

Meaning: I think that in some cases, valgrind is actually tracking the memcpy of uninitialized bytes and then alerting you later when you access those secondary uninitialized bytes.

If I'm right, you can memset the local_hostname buffer (or use calloc), and then valgrind warnings will go away.