stackalloc をいつ使用するかについて、以前の StackOverflow の投稿から例を読みました。さて、この例は私を少し当惑させました:
public unsafe void DoSomeStuff()
{
byte* unmanaged = stackalloc byte[100];
byte[] managed = new byte[100];
//Do stuff with the arrays
//When this method exits, the unmanaged array gets immediately destroyed.
//The managed array no longer has any handles to it, so it will get
//cleaned up the next time the garbage collector runs.
//In the mean-time, it is still consuming memory and adding to the list of crap
//the garbage collector needs to keep track of. If you're doing XNA dev on the
//Xbox 360, this can be especially bad.
}
私はまだ C# とプログラミング全般の初心者なので、間違っている場合は遠慮なく訂正してください。しかし、バイトは値型ではありませんか? また、値型は宣言された場所に格納されていませんか? これは、この例でmanaged
は もスタックに格納されているということではないでしょうか。拡張により、このスタック フレームが終了し、呼び出しアドレスに移動すると、メモリは自動的にクリーンアップされるため、これmanaged
と同じ方法で削除する必要があります。unmanaged
例?