Когда на стеке выделяется массив фиксированного размера?
-
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
Переменная декларация, но я не хочу делать это, если я не должен.
Решение
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