i need to create a variable length array of unmanaged object, but i need the array to be unmanaged too, so i can't use List<> or other classes but i have to malloc/realloc it in the c way.
this is my code:
unsafe class A{
unsafe private int a1, a2;
unsafe public byte* arr;
}
and on a different class method i would like to do something like this:
private unsafe A* arr = Marshal.AllocHGlobal(array_size * Marshal.SizeOf<A>());
Now i these questions:
- How to convert IntPtr to A*? OR does exist a proper c# malloc/realloc function? OR if i .dll import the real c/c++ realloc function would be safe to use it?
- How do i get a A* pointer to an A unmanaged object? if i try with '&' it says "cannot take the adress of [...] a managed object" but it shouldn't be managed. Am I doing something wrong in class declaration?
optional: why sizeof(A) doesn't work but Marshal.SizeOf() it's fine?
Ais not unmanaged, it's just regular .NET class (marking it with "unsafe" keyword doesn't change anything, and is redundant).