0

This question is related to a prior question of mine. I used the code below to inspect the values in the streambuf object associated with cout. Using VS2010 IDE, I can see several members in this class. Could anyone point to me which one of these members points to cout's null buffer in memory ?

#include <iostream>

int main()
{
    std::streambuf* p = std::cout.rdbuf();  
}

Edit: Looking at the streambuf header file one can find the following private members in the basic_streambuf class:

    _Mutex _Mylock; // thread lock
    _Elem *_Gfirst; // beginning of read buffer
    _Elem *_Pfirst; // beginning of write buffer
    _Elem **_IGfirst;   // pointer to beginning of read buffer
    _Elem **_IPfirst;   // pointer to beginning of write buffer
    _Elem *_Gnext;  // current position in read buffer
    _Elem *_Pnext;  // current position in write buffer
    _Elem **_IGnext;    // pointer to current position in read buffer
    _Elem **_IPnext;    // pointer to current position in write buffer

    int _Gcount;    // length of read buffer
    int _Pcount;    // length of write buffer
    int *_IGcount;  // pointer to length of read buffer
    int *_IPcount;  // pointer to length of write buffer

    locale *_Plocale;   // pointer to imbued locale object

I believe _Pfirst is the address I'm looking for and it's NULL as expected.

Community
  • 1
  • 1
John Kalane
  • 1,163
  • 8
  • 17
  • Are you trying to access the put-area of the stream buff? The only documented way I'm aware of is via the members [pbase(), pptr() and epptr()](http://en.cppreference.com/w/cpp/io/basic_streambuf/pptr). The actual physical members held within a std::streambuf implementation will be implementation dependent to the best of my knowledge – WhozCraig Feb 10 '13 at 19:43
  • @WhozCraig I understand this. I just want to confirm that the streambuf member that points to the actual memory buffer is NULL, as cout is unbuferred in MS implementations. – John Kalane Feb 10 '13 at 20:07
  • You can't really rely on that one way or another. How that buffer (if any) is used is still entirely up to the implementation. Were I writing I would use the default implementation for building the text, then just flush it after every insertion, which would clearly throw salt in the face of your conjecture as reliable. If you're not concerned about portability and you've done your homework on the members from the lib-src from MS, (which you've done) thats possible. If this is multi-platoform, then the whole operation sits in a `#ifdef _MSC_VER` anyway, so its not like i will matter in the end. – WhozCraig Feb 10 '13 at 21:03

0 Answers0