スタックに割り当てられた固定サイズの配列はいつですか。
-
15-11-2019 - |
質問
ソケットストリームからディスクへのバイトをコピーする方法は次のとおりです。
public static void CopyStream(Stream input, Stream output)
{
// Insert null checking here for production
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
.
私が興味があるのは次のとおりです.buffer
はスタック上または上に割り当てられます。
ヒープ?確かに、このメソッドを安全にすることができ、fixed
キーワードを追加することができます。
変数宣言ですが、私はそうする必要がないIFNをやりたくありません。
解決
The buffer
variable will be allocated on the stack, the 8192 byte memory the buffer
variable holds the location of will be on the heap.
why are you talking about fixed
? Are you trying to speed things up? It almost certainly won't...
To quote Eric Lippert:
"But in the vast majority of programs out there, local variable allocations and deallocations are not going to be the performance bottleneck. "
Ref.
所属していません StackOverflow