struct - What is right way to work with fixed size buffers (arrays) in C#? -
i have following unmanaged (c++) struct:
struct parsed_data_struct { int some_number; float some_array[4]; };
and have call function (in c++ dll) fill array of structs values:
void parse_data (parsed_data_struct* parsed_data);
i've found way make structs array blittable using fixed size buffers
so managed struct is:
[structlayout(layoutkind.sequential, pack = 1)] public unsafe struct parseddatastruct { public int some_number; public fixed float some_array[4]; }
and calling code is
public void parse(parseddatastruct[] parseddata) { unsafe { fixed (parseddatastruct* parseddataptr = parseddata) { parse_data(parseddataptr); } } }
official documentation , other resources require fixing structs array work them. i've saw there example of accessing kind array without fixing it.
so have questions:
- is example right? there enough fixing of array of structs?
- is there need fix structs array access elements?
as far remember, don't need use unsafe , pointers in such situation in c#. there problem in code. in c++ code. struct (shouldn't class?) in c# packed. struct in c++ not. should pack too:
#pragma pack(push, 1) //struct definition here #pragma pack(pop)
Comments
Post a Comment