简体   繁体   English

何时将内存分配给预定义的流对象?

[英]When memory gets allocated to predefined stream objects?

Before you start to mark this question as duplicate I've already this but it doesn't answer my question. 开始之前,以纪念这个问题,因为重复我已经这样 ,但它并没有回答我的问题。

stream objects like std::cout , std::cin are global instances of ostream & istream classes. std::coutstd::cin这样的流对象是ostreamistream类的全局实例。 But my question is when memory is allocated to these objects? 但是我的问题是何时将内存分配给这些对象? When these objects are initialized? 这些对象何时初始化? Is memory allocated at compile time or runtime or C++ runtime initialize these objects at runtime before calling main()? 是在编译时,运行时或C ++运行时分配的内存在调用main()之前在运行时初始化这些对象吗? Where std::cout & std::cin object resides: in the stack, heap or data segment? std::coutstd::cin对象驻留在哪里:位于堆栈,堆或数据段中?

The stream objects are global variables, so they will reside in the global data segment. 流对象是全局变量,因此它们将驻留在全局数据段中。 They might also internally allocate other memory for buffers, or whatever they need. 他们还可能在内部为缓冲区或其他所需的内存分配其他内存。

They are initialized, by some unspecified magic (= possibly implementation specific tricks), as early as possible, but no later than before the first statement of main. 它们由某些未指定的魔术(可能是实现特定的技巧)尽早初始化,但不得早于main的第一条语句之前进行初始化。

when memory is allocated to these objects? 什么时候将内存分配给这些对象?

At the same time as to other global objects with static storage duration 同时具有静态存储持续时间的其他全局对象

When these objects are initialized? 这些对象何时初始化?

When other static objects are intitialized, but before you will have chance to use them due to standard mandate and library tricks. 初始化其他静态对象后,由于标准的授权和库技巧,您将有机会使用它们。

Where std::cout & std::cin object resides: in the stack, heap or data segment? std :: cout和std :: cin对象驻留在哪里:位于堆栈,堆或数据段中?

They reside in static memory . 它们驻留在静态内存中 Strictly speaking there is no stack or heap in C++, only static, dynamic and automatic memory. 严格来说,C ++中没有堆栈 ,只有静态,动态和自动内存。 Exact place would be dependend on library implementation. 确切的位置取决于库的实现。

These objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed and are available for use in the constructors and destructors of static objects. 确保在第一次构造std :: ios_base :: Init类型的对象期间或之前初始化这些对象,并且可在静态对象的构造函数和析构函数中使用这些对象。

This is LLVM implementation: https://github.com/llvm-mirror/libcxx/blob/master/src/iostream.cpp 这是LLVM的实现: https : //github.com/llvm-mirror/libcxx/blob/master/src/iostream.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM