小编ano*_*cow的帖子

D中基于堆栈的对象实例化

我正在学习D,并且因为我得到的错误而感到困惑.

考虑以下:

module helloworld;

import std.stdio;
import std.perf;

ptrdiff_t main( string[] args )
{
     auto t = new PerformanceCounter;    //From managed heap
     //PerformanceCounter t;             //On the stack

     t.start();
     writeln( "Hello, ", size_t.sizeof * 8, "-bit world!" );
     t.stop();

     writeln( "Elapsed time: ", t.microseconds, " \xb5s." );

     return 0;
} //main()
Run Code Online (Sandbox Code Playgroud)

产量非常可观:

Hello, 32-bit world!
Elapsed time: 218 µs.
Run Code Online (Sandbox Code Playgroud)

现在考虑当我尝试在堆栈上初始化PerformanceCounter而不是使用托管堆时会发生什么:

 //auto t = new PerformanceCounter;  //From managed heap
 PerformanceCounter t;               //On the stack
Run Code Online (Sandbox Code Playgroud)

产量:

--- killed by signal 10
Run Code Online (Sandbox Code Playgroud)

我很难过.有什么想法为什么这打破?(Mac OS …

stack d allocation raii phobos

6
推荐指数
1
解决办法
625
查看次数

标签 统计

allocation ×1

d ×1

phobos ×1

raii ×1

stack ×1