I have the following method to copy bytes from a socket stream to disk:
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);
}
}
What I am curious about is: will buffer
be allocated on the stack or on the
heap? To be sure, I could make this method unsafe, and add the fixed
keyword to
the variable declaration, but I don't want to do that ifn I don't have to.
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.