next up previous contents
Next: Statically-Allocated string data Up: Allocation Options Previous: Heap-based string data   Contents

Stack-based string data

Stack-based string data starts on the stack and uses the stack for as long as possible. If and when an event occurs that would cause a stack buffer overrun, buffer data is automatically moved to the heap. One subtle point from this is that you should always ask a string for its buffer data directly.

There are two ways to create stack-based data. The first is to use the attach() function. The second is to use the Str() constructor that calls attach() as a part of its implementation. The constructor is shown here. The attach() function is detailed in chapter 6.

// One way
char buff[32];
Str x(buff, sizeof(buff));

// Use The Macro for the same result
STR(x,32);

// Another way, this keeps the initialized data
char *buff = "Hello World";
Str x(buff, sizeof(buff), false);



2007-05-05