dimanche 28 mai 2023

Does snprintf clobber the buffer after null-terminator?

Consider the following code:

char buffer[5]{};
snprintf(buffer, sizeof(buffer), "hi");

snprintf writes 'h', 'i' and '\0' to buffer.

My question is: is there any guarantee that the snprintf won't touch buffer[3] and buffer[4] according to the standard? Visual Studio 2019 keeps the unused buffer untouched, but is this a standard behavior?

Aucun commentaire:

Enregistrer un commentaire