【发布时间】:2017-11-29 19:29:47
【问题描述】:
我在 GENIVI/capicxx-core-runtime 中遇到了一个名为 Runtime 的类。下面指定的部分代码。
class Runtime {
public:
COMMONAPI_EXPORT static std::shared_ptr<Runtime> get();
COMMONAPI_EXPORT Runtime();
COMMONAPI_EXPORT virtual ~Runtime()
private:
static std::shared_ptr<Runtime> theRuntime__;
};
std::shared_ptr<Runtime> Runtime::theRuntime__ = std::make_shared<Runtime>();
std::shared_ptr<Runtime> Runtime::get() {
return theRuntime__;
}
我怀疑theRuntime__ 变量。由于shared_ptr进行堆分配,那么theRuntime__变量的底层对象在这种情况下是分配在堆上还是分配在BSS/DATA段中?
【问题讨论】:
-
如果“底层对象”是指
theRuntime__指向的Runtime实例,那么它是由std::make_shared<Runtime>()调用在堆上分配的。 -
shared_ptr 指向的对象的内存取自堆,但
theRuntime__对象的内存分配到 BSS 段 - 这是静态对象。
标签: c++ c++11 static shared-ptr