Stackalloc arrays in C #

I am trying to get some information about stack arrays in C #, and the only way to allocate this type of buffer is to use unsafe code

char* buffer = stackalloc char[16];

As a workaround, I can create a character structure.

struct Buffer16
{
   public char c1,c2 //to 16
}

Is there a way to make the buffer not by creating a new type. Create at runtime.

+3
source share
2 answers

You can always point to pointers or structures of a primitive type:

char* buffer = stackalloc char[16];
int* i = (int*) buffer;
long* l = (long*) buffer;
byte* b = (byte*) buffer;
Point* p = (Point*) buffer;

So you have all the flexibility. You can also use the Marshal.PtrToStructurereverse without using unsafe code.

Does this answer your question?

+3
source

, , .

#, , , CLR , . , , , , , , . , , , , , .

+1

Source: https://habr.com/ru/post/1776578/


All Articles