Когда на стеке выделяется массив фиксированного размера?

StackOverflow https://stackoverflow.com/questions/6053115

Вопрос

У меня есть следующий метод для копирования байтов из потока сокета на диск:

 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top