Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

C/C++ API Documentation | 2025.2

# StrBuf::StringInit()

Initialize a `StrBuf`.

Virtual? | No |
Class | `StrBuf` |
Arguments | None |
Returns | `void` |

## Notes

`StringInit()` initializes the `StrBuf` to contain a zero-length null buffer.

Normally when a `StrBuf` is created, it is initialized using the `StrBuf` constructor. However, there might be specialized cases where memory has already been allocated for a `StrBuf` instance, but the memory was not allocated through the normal mechanisms that would result in the `StrBuf` constructor initializing the instance. For these specialized cases, `StringInit()` is appropriate for initializing a `StrBuf` instance.

After a `StrBuf` has been used, calling `StringInit()` for the instance can result in a memory leak. Specifically, once the `buffer` member has been pointed to memory other than `nullStrBuf`, calling `StringInit()` for the instance will abandon the memory.

In most cases, it is preferable to use an alternative such as one of the following:

    sb1 = StrRef::Null();

    sb2.Clear();
    sb2.Terminate();

    sb3.Set( "" );

    sb4 = "";

## See also

`StrBuf::Clear()` `StrBuf::Set()` `StrBuf::Terminate()` `StrBuf::operator =( char * )` `StrRef::Null()`

## Example

Copy

    #include &lt;iostream&gt;
    #include &lt;errno.h&gt;

    #include &lt;stdhdrs.h&gt;
    #include &lt;strbuf.h&gt;

    #define NSTRBUFS     5
    #define CHUNKSIZE    1024
    #define STRBUFSIZE   sizeof( StrBuf )

    int main( int argc, char **argv )
    {
        char chunk[ CHUNKSIZE ];
        int chunkFree = CHUNKSIZE;
        char *pchunkStart = &amp;chunk[ 0 ];
        char *pchunk;

        int iStrBuf;

        // Initialize the StrBufs in the chunk.

        for ( iStrBuf = 0, pchunk = pchunkStart;
             iStrBuf &lt; NSTRBUFS;
             iStrBuf, pchunk += STRBUFSIZE )
        {
            // Ensure that there's enough free left in the chunk for a StrBuf.
            if ( (chunkFree -= STRBUFSIZE) &amp;lt; 0)
            {
                cout &amp;lt;&amp;lt; "Not enough free left in the chunk!\n";
                return ENOMEM;
            }

            // Initialize and set the value of the StrBuf.

            ((StrBuf *)pchunk)-&amp;gt;StringInit();
            *(StrBuf *)pchunk &amp;lt;&amp;lt; iStrBuf + 73;
        }

        // Print the StrBufs. Do this in a separate loop so as to provide
        // some evidence that the above loop didn't corrupt adjacent StrBufs.
        for ( iStrBuf = 0, pchunk = pchunkStart;
              iStrBuf &amp;lt; NSTRBUFS;
              iStrBuf, pchunk += STRBUFSIZE )
        {
            cout &lt;&lt; "StrBuf " &lt;&lt; iStrBuf + 1 &lt;&lt; " contains \"";
            cout &lt;&lt; ((StrBuf *)pchunk)-&gt;Text() &lt;&lt; "\"\n";
        }
    }

Executing the preceding code produces the following output:

    StrBuf 1 contains "73"
    StrBuf 2 contains "74"
    StrBuf 3 contains "75"
    StrBuf 4 contains "76"
    StrBuf 5 contains "77"

Your search for returned results