# std::basic_streambuf<CharT,Traits>::setp

> C++ Atlas: `cppatlas.com/w/cpp/io/basic_streambuf/setp` | Source: https://en.cppreference.com/w/cpp/io/basic_streambuf/setp | Domain: Input/output

Sets the values of the pointers defining the put area.

## Official Specification Summary (cppreference)

```cpp
protected:
void setp( char_type* pbeg, char_type* pend );
```

Sets the values of the pointers defining the put area.

After the call, `pbase() == pbeg`, `pptr() == pbeg` and `epptr() == pend` are all `true`.

If any of `[pbeg,` `pend)` is not a valid range, the behavior is undefined.

### Parameters

- `pbeg` - pointer to the new beginning of the put area
- `pend` - pointer to the new end of the put area

### Example

```cpp
#include <array>
#include <cstddef>
#include <iostream>

// Buffer for std::ostream implemented by std::array
template<std::size_t size, class CharT = char>
struct ArrayedStreamBuffer : std::basic_streambuf<CharT>
{
    using Base = std::basic_streambuf<CharT>;
    using char_type = typename Base::char_type;

    ArrayedStreamBuffer()
    {
        // put area pointers to work with “buffer”
        Base::setp(buffer.data(), buffer.data() + size);
    }

    void print_buffer()
    {
        for (char_type i : buffer)
        {
            if (i == 0)
                std::cout << "\\0";
            else
                std::cout << i;
            std::cout << ' ';
        }
        std::cout << '\n';
    }

private:
    std::array<char_type, size> buffer{}; // value-initialize “buffer”
};

int main()
{
    ArrayedStreamBuffer<10> streambuf;
    std::ostream stream(&streambuf);

    stream << "hello";
    stream << ",";

    streambuf.print_buffer();
}
```

Output:

```text
h e l l o , \0 \0 \0 \0
```

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 4023 | C++98 | `setp` did not require the output sequence to be a valid range | requires |

### See also

- [`setg`](https://en.cppreference.com/w/cpp/io/basic_streambuf/setg): repositions the beginning, next, and end pointers of the input sequence (protected member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/basic_streambuf/setp (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.