cout internal

cout internal

cout internal

先看看cout是这么定义的: istream cout(&fout)

//C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\cout.cpp

// cout -- initialize standard output stream
#include <fstream>
#include <iostream>

#pragma warning(disable: 4074)
#pragma init_seg(compiler)
static std::_Init_locks  initlocks;

_STD_BEGIN
        // OBJECT DECLARATIONS

__PURE_APPDOMAIN_GLOBAL static filebuf fout(_cpp_stdout);
#if defined(_M_CEE_PURE)
__PURE_APPDOMAIN_GLOBAL extern ostream cout(&fout);
#else
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream cout(&fout);
#endif
        // INITIALIZATION CODE
struct _Init_cout
    {   // ensures that cout is initialized
    __CLR_OR_THIS_CALL _Init_cout()
        {   // initialize cout
        _Ptr_cout = &cout;
        if (_Ptr_cin != 0)
            _Ptr_cin->tie(_Ptr_cout);
        if (_Ptr_cerr != 0)
            _Ptr_cerr->tie(_Ptr_cout);
        if (_Ptr_clog != 0)
            _Ptr_clog->tie(_Ptr_cout);
        }
    };
__PURE_APPDOMAIN_GLOBAL static _Init_cout init_cout;

_STD_END

/*
 * Copyright (c) 1992-2007 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.03:0009 */

再看看 _cpp_stdout 是怎么回事?

//C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\yvals.h

   #define _cpp_stdin  (&(__iob_func())[0])
   #define _cpp_stdout (&(__iob_func())[1])
   #define _cpp_stderr (&(__iob_func())[2])
   #define _cpp_isleadbyte(c) (__pctype_func()[(unsigned char)(c)] & _LEADBYTE)

关于 __iob_func(): __iob_func() returns a pointer to the array of FILE descriptors that holds stdin, stdout, stderr and any FILE objects opened through the C runtime library. See the MSVC runtime library source _file.c.

//C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\_file.c

/*
 * FILE descriptors; preset for stdin/out/err (note that the __tmpnum field
 * is not initialized)
 */
FILE _iob[_IOB_ENTRIES] = {
        /* _ptr, _cnt, _base,  _flag, _file, _charbuf, _bufsiz */

        /* stdin (_iob[0]) */

        { _bufin, 0, _bufin, _IOREAD | _IOYOURBUF, 0, 0, _INTERNAL_BUFSIZ },

        /* stdout (_iob[1]) */

        { NULL, 0, NULL, _IOWRT, 1, 0, 0 },

        /* stderr (_iob[3]) */

        { NULL, 0, NULL, _IOWRT, 2, 0, 0 },

};

/*
 * Initializer and terminator for stdio
 */
int  __cdecl __initstdio(void);
void __cdecl __endstdio(void);

_CRTALLOC(".CRT$XIC") static _PIFV pinit = __initstdio;

_CRTALLOC(".CRT$XPXA") static _PVFV pterm = __endstdio;
//C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\internal.h

struct _iobuf {
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
        };
typedef struct _iobuf FILE;

Post by: Jalen Wang (转载请注明出处)

posted on 2013-08-04 10:00  Jalen Wang  阅读(686)  评论(0编辑  收藏  举报

导航