struct

Proper way to get a mutable struct for Memory<byte> / Span<byte>?

社会主义新天地 提交于 2019-12-30 07:02:10
问题 For a network protocol implementation I want to make use of the new Memory and Span classes to achieve zero-copy of the buffer while accessing the data through a struct . I have the following contrived example: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Data { public int IntValue; public short ShortValue; public byte ByteValue; } static void Prepare() { var buffer = new byte[1024]; var dSpan = MemoryMarshal.Cast<byte, Data>(buffer); ref var d = ref dSpan[0]; d.ByteValue = 1

How to properly define a function pointer in struct, which takes struct as a pointer?

丶灬走出姿态 提交于 2019-12-30 06:28:07
问题 I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such that is will compile without warnings? typedef struct { // some fields required for processing... int (*doAction)(struct pr_PendingResponseItem *pr); } pr_PendingResponseItem; If I remove the "struct" attribute on the pr parameter, I get an error. If I leave it in, I get a warning: "its scope is only this definition or

Any reason to prefer memset/ZeroMemory to value initialization for WinAPI structs?

吃可爱长大的小学妹 提交于 2019-12-30 05:59:10
问题 In Win32 programming a handful of POD structs is used. Those structs often need to be zeroed out before usage. This can be done by calling memset() / ZeroMemory() STRUCT theStruct; ZeroMemory( &theStruct, sizeof( theStruct ) ); or by value initialization: STRUCT theStruct = {}; Although the two variants above are not equivalent in general: treat padding differently treat non-POD member variables differently in case of POD structs used in Win32 they look equivalent. Are there any cases when

Getting Garbage Values while reading struct data from a binary file

≯℡__Kan透↙ 提交于 2019-12-30 05:32:07
问题 Hi guys in my previous question, I was able to get the data of a struct to be loaded on a file, but now the issue is I'm getting garbage value while retrieving it. File Contents: settings.bin 110#NormalCompression Level210#NormalCompression Level310#NormalCompression Level410#NormalCompression Level510#NormalCompression Level Code #include<cstdlib> #include<iostream> #include<string> #include<iomanip> #include<fstream.h> using namespace std; const char* ErrorLogFilePath = "resources\\error

How to programmatically get the number of fields of a struct?

天涯浪子 提交于 2019-12-30 03:44:48
问题 I have a custom struct like the following: struct MyStruct { first_field: i32, second_field: String, third_field: u16, } Is it possible to get the number of struct fields programmatically (like, for example, via a method call field_count() ): let my_struct = MyStruct::new(10, "second_field", 4); let field_count = my_struct.field_count(); // Expecting to get 3 For this struct: struct MyStruct2 { first_field: i32, } ... the following call should return 1 : let my_struct_2 = MyStruct2::new(7);

Go : When will json.Unmarshal to struct return error?

依然范特西╮ 提交于 2019-12-30 03:11:04
问题 Assume i have a struct like type A struct{ name string`json:"name"` } Then in main i have code var jsonString string = `{"status":false}` var a A error := json.Unmarshal([]byte(jsonString),&a) apparently the code above produce a nil error, regardless the json format is different. When will json.Unmarshal() return error in Go? 回答1: The JSON decoder does not report an error if values in the source do not correspond to values in the target. For example, it's not an error if the source contains

Struct inside struct

我怕爱的太早我们不能终老 提交于 2019-12-30 03:10:06
问题 I must create a Person and each Person should have a Fridge. Is this the best way of doing it? If so what am I doing wrong? Thanks in advance. typedef struct { int age; struct FRIDGE fridge; } PERSON; typedef struct { int number; } FRIDGE; FRIDGE fr; fr.number=1; PERSON me; me.name=1; me.fridge = fr; This gives me the following error: error: field ‘fridge’ has incomplete type 回答1: struct FRIDGE is something different than FRIDGE . You need to either use type FRIDGE in your other structure.

Aliasing struct and array the C++ way

非 Y 不嫁゛ 提交于 2019-12-30 01:37:07
问题 This is a C++ followup for another question of mine In the old days of pre-ISO C, the following code would have surprised nobody: struct Point { double x; double y; double z; }; double dist(struct Point *p1, struct Point *p2) { double d2 = 0; double *coord1 = &p1->x; double *coord2 = &p2->x; int i; for (i=0; i<3; i++) { double d = coord2[i] - coord1[i]; // THE problem d2 += d * d; } return sqrt(d2); } Unfortunately, this problematic line uses pointer arithmetic ( p[i] being by definition *(p

Named Parameters in Ruby Structs

南笙酒味 提交于 2019-12-30 01:37:07
问题 I'm pretty new to Ruby so apologies if this is an obvious question. I'd like to use named parameters when instantiating a Struct, i.e. be able to specify which items in the Struct get what values, and default the rest to nil. For example I want to do: Movie = Struct.new :title, :length, :rating m = Movie.new :title => 'Some Movie', :rating => 'R' This doesn't work. So I came up with the following: class MyStruct < Struct # Override the initialize to handle hashes of named parameters def

Extract the fields of a C struct

喜夏-厌秋 提交于 2019-12-29 17:53:07
问题 I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the struct or ctypes modules. So I'll have a .h file full of struct definitions, and I have to manually read through them and duplicate those definitions in my Python code. This is time consuming and error-prone, and it's difficult to keep the two definitions in sync when they change frequently. Is there some tool or library in any language (doesn't have to be C or