Outputting an Unterminated Buffer

Properly formed strings in C are terminated with the null character, \0. Accept it or die!

However . . .

It does happen, occasionally, that a chunk of memory is formed that holds text in an unterminated state. In this situation, you must either code a method to output the text or properly terminate the string so that it behaves like other strings.

For example, data received in a network packet appears in a buffer unterminated. As a programmer, you’ve given the buffer’s address and the size of the data received. These values, the buffer and the data size, are another way to represent a string, though what you want is the data size to be less than the buffer size so that you can properly convert the text into a string. (When the buffer is full, no room is available to append the terminating null character.)

The key to converting the buffer’s character data into text, or just outputting the text directly, is knowing the character count. This tidbit is provided in a network packet and what I simulate in this Lesson: An 18-byte buffer is filled with ten characters. No terminated null character is added, but the character count is known, stored in an int constant. This character count is key to converting the characters into a string.

This first example shows how you can output the text, though it doesn’t convert the characters into a string:

2022_04_30-Lesson-a.c

#include <stdio.h>

int main()
{
    const int size=10;    /* number of characters in the buffer */
    char buffer[size+8];    /* larger buffer size */
    int x;

    /* fill the buffer - not a string */
    for( x=0; x<size; x++ )
        buffer[x] = 'A'+x;

    /* output the buffer */

    for( x=0; x<size; x++ )    /* one character at a time */
        putchar(buffer[x]);
    putchar('\n');            /* convenient newline */

    return(0);
}

The putchar() function at Line 16 pops out one character at time. You could also perform the same feat by using a single printf() function:

2022_04_30-Lesson-b.c

#include <stdio.h>

int main()
{
    const int size=10;    /* number of characters in the buffer */
    char buffer[size+8];    /* larger buffer size */
    int x;

    /* fill the buffer - not a string */
    for( x=0; x<size; x++ )
        buffer[x] = 'A'+x;

    /* output the buffer */

    printf("%.*s\n",size,buffer);

    return(0);
}

The printf() format string uses the * variable width placeholder to set the string’s output width, using the size variable in the argument list. The characters are output, but they remain in the buffer without being converted into a string.

This final trick converts the text into a true string, thanks to the code knowing the number of characters in the buffer.

2022_04_30-Lesson-c.c

#include <stdio.h>

int main()
{
    const int size=10;    /* number of characters in the buffer */
    char buffer[size+8];    /* larger buffer size */
    int x;

    /* fill the buffer - not a string */
    for( x=0; x<size; x++ )
        buffer[x] = 'A'+x;

    /* output the buffer */

    buffer[size] = '\0';    /* cap the string */
    puts(buffer);            /* output the string */

    return(0);
}

The assignment at Line 15 caps the string. Offset size sets the null character’s position in the buffer. A string is created, which is then dealt with like any string. Here is the output — identical for each code sample in this Lesson:

ABCDEFGHIJ

The only problem with the last example is that it’s possible to overflow the buffer when the number of characters in the buffer matches the buffer size. This reason is why many coders prefer the preceding two techniques to just output the text over capping the text and creating a string within the buffer. The choice is up to you.

Leave a Reply