byte[] buffer = new byte[500000];
initializes buffer with 0 values. As it is a buffer, I dont want any initialization, is it possible in C# as in C?
I don't think it is possible... Even FormatterServices.GetUninitializedObject that doesn't run constructors:
Because the new instance of the object is initialized to zero and no constructors are run, the object might not represent a state that is regarded as valid by that object.
Note that if you want unmanaged (memory taken from the OS that isn't GC-managed), that can be allocated without zeroing it, but it wouldn't be a byte[].
fixed to access the memory before it is initialized, I don't think it works :-) I think that it is the OS that allocates the memory when requested (and cleans it), not the .NET .A way in C# to do what malloc does in C is to use Marshal.AllocHGlobal in an unsafe context:
unsafe
{
var ptr = Marshal.AllocHGlobal(50000 * sizeof(int));
int* values = (int*)ptr;
// lists uninitialized ints
for (int i = 0; i < 50000; i++)
Console.WriteLine(values[i]);
Marshal.FreeHGlobal(ptr);
}
Fill it with random numbers or use DllImport and VirtualAlloc from Win32 API.
GlobalAlloc + GlobalLock to alloc uninitialized memory. It can be used as source of "random" memory. I suspect the author want to catch values in RAM from other processes...HeapAlloc to do it.
bufferto benull?0is the default value forbyte, what would you actually want to happen?malloc) I don't think it is possible.