Quando è un array di dimensioni fisse assegnato sullo stack?
-
15-11-2019 - |
Domanda
Ho il seguente metodo per copiare byte da un flusso di socket su disco:
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);
}
}
.
Di cosa sono curioso di curioso è: buffer
sarà assegnato sullo stack o sul
mucchio?Per essere sicuro, potrei rendere questo metodo pericoloso e aggiungere la parola chiave fixed
a
La dichiarazione variabile, ma non voglio farlo se non devo.
Soluzione
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.