[C++ Notes] The meaning of the return value of snprintf() function

The snprintf() function is a very commonly used function, and its function is to write data into an array (or string). Sprintf() is not used because it is not safe enough. If you are not careful, the memory will overflow and cause a "segment fault"! I have known snprintf() in the code for a long time, but I have not paid attention to its return value (I always thought that the return value is the same as sprintf as the number of bytes successfully written, error). I checked the information today and fully understood the meaning of its return value. First look at the declaration of snprintf() function:

int snprintf(char *str, size_t size, const char *format, ...);

Regarding the function of the function, it shouldn’t be said any more. Well, let’s look at an example first.
[C++ Notes] The meaning of the return value of snprintf() function
[C++ Notes] The meaning of the return value of snprintf() function
There is no doubt about the results of the operation, but if the size of buf is smaller than the number of bytes to be written, if the string to be written is "0123456789bcdef" (16 bytes, not the null character'\0'), the size of buf is still 10 ,Look at the return value of the snprintf() function.
[C++ Notes] The meaning of the return value of snprintf() function
[C++ Notes] The meaning of the return value of snprintf() function
It can be seen from the above two examples that the return value of the snprintf() function is "the length of the string to be written (not including the'\0' at the end of the string)", regardless of whether buf can hold this string. In other words, this value can be equal to size or even larger than size! Take the above example, if the writable string is "0123456789bcdef" with a total of 16 bits, even if the size of buf is limited to 10, the return value of snprintf() will be 16 instead of 10! Check the documentation (man 3 snprintf) description also proves this.
[C++ Notes] The meaning of the return value of snprintf() function
The above content also says that if the return value is equal to or greater than size, it indicates that the output string is truncated. This feature is often used in projects for abnormal judgment. The following is the source code of the network framework used in the project. It is precisely because of this code that I have to consult relevant information to figure out this little detail.
[C++ Notes] The meaning of the return value of snprintf() function

Recommended reading:

Carefully organized | The article catalog
in the second half of 2017 Deeply understand the log mechanism in the system (Part 2)
[C++ Notes] Understand the dual meaning of typename
[C++ Notes] Memory layout of C++ object model (2)

Focus on server background technology stack knowledge summary sharing

Welcome to pay attention to communication and common progress

[C++ Notes] The meaning of the return value of snprintf() function

Coding

The code farmer has the right way to provide you with easy-to-understand technical articles to make technology easier!

Guess you like

Origin blog.51cto.com/15006953/2552122