struct

How does the heap and stack work for instances and members of struct in C#?

我是研究僧i 提交于 2021-02-02 08:38:01
问题 I'm reading a book which says: The variable representing an struct instance doesn’t contain a pointer to an instance; the variable contains the fields of the instance itself. Because the variable contains the instance’s fields, a pointer doesn’t have to be dereferenced to manipulate the instance’s fields. The following code demonstrates how reference types and value types differ class SomeRef { public Int32 x; } struct SomeVal { public Int32 x; } static void ValueTypeDemo() { SomeRef r1 = new

Struct alignment suggestions?

喜欢而已 提交于 2021-01-29 17:06:40
问题 I am sending data over a network. To send that data over a network I am packing it into a byte array. Right now the data type consist of a byte and 3 floats making it 13 bytes total. To read this I need to know the size of the structure. Using Marshal's sizeOf does not return the "correct" size of my serialized byte array. Now I know what is happening. Marshal is returning me the managed aligned size of my struct. I also know if I flag it [StructLayout(LayoutKind.Sequential, Pack = 1)] then

Push of stack not inserting the new value - C

ε祈祈猫儿з 提交于 2021-01-29 16:24:36
问题 void push(stack *head, int valuee) { if(head->next==NULL && head->value==-1) { head->value = valuee; printf("First element %d inserted\n",valuee); } else { stack *temp = new stack; temp->value = valuee; temp->next = head; head = temp; printf("Element %d inserted\n",valuee); } } First element is inserted properly but when i continue inserting elements, none of the elements are inserted after the first one. Read somewhere that i have to pass pointer to pointer of stack but I did this same thing

Spread operator analogue

狂风中的少年 提交于 2021-01-29 12:49:36
问题 I have a struct and the instance of that struct: type Obj struct { ssid string code string mit string // and other props (23) } var ValidObject = Obj { ssid: "AK93-KADJ9-92J76", code: "SKO-120O" mit: "MSLA-923-OKSW" } I want to create a slice of structs ( Obj ) which will contain ValidObject with only some fields changed. I think the best way to explain that would be using pseudo code, so here it is (using spread operator from JS :) ): var slc = []Obj{ { ...ValidObject, code: "Other value", }

C printing extremely weird values with printf

▼魔方 西西 提交于 2021-01-29 12:32:20
问题 I am rewriting this post because I managed to figure out the problem. The problem with my extremely broken output was due to an improper dynamic memory allocation. Basically I needed to allocate memory for an array of pointers that pointed to structs, but the array itself was nested inside of another struct and the nesting confused me slightly and I ended up over complicating it. So I had a struct named Catalog, that my array was in and that array pointed to another struct named Books. When I

Creating Decode Path from JSON Data in Swift that Includes Numbers and Hyphens?

与世无争的帅哥 提交于 2021-01-29 09:32:13
问题 this is relatively new to me and I've searched high and low but have been unsuccessful in finding a similar scenario. I have retrieved some JSON Data from an API URL and have successfully decoded and output various values from this data as strings by parsing the data to a separate sheet and using structs and constants with the 'Decodable' value set. The problem I have is that one of the containers in the Json data is a hyphenated date in this format dates['2020-11-04'] so swift will not let

How to send pointer in struct in MPI

谁说胖子不能爱 提交于 2021-01-29 08:07:17
问题 I have struct like this: typedef struct { int x; double *y; int **z; } ind; how could I send pointer like *y and **z via MPI to other processes? I know that many answers said that never send pointers by MPI. But if I cannot change *y to an array because it is used in other part of the main program, what should I do to transfer them through processes via MPI? Especially for **z, how should I do ? Thanks in advance! 回答1: Just following the code from the second example here, I did the following.

How to send pointer in struct in MPI

半世苍凉 提交于 2021-01-29 08:06:16
问题 I have struct like this: typedef struct { int x; double *y; int **z; } ind; how could I send pointer like *y and **z via MPI to other processes? I know that many answers said that never send pointers by MPI. But if I cannot change *y to an array because it is used in other part of the main program, what should I do to transfer them through processes via MPI? Especially for **z, how should I do ? Thanks in advance! 回答1: Just following the code from the second example here, I did the following.

Flexible array error with gcc 6 but not with gcc 4

柔情痞子 提交于 2021-01-29 04:20:43
问题 I'm trying to compile a piece of code written by a colleague. On my machine with gcc 6.1.1 I get this error: In file included from src/TYAGlobalLibs.cc:1:0: ./include/TYAGlobalLibs.hh:112:17: error: flexible array member ‘TYA::TMultiHist::fh’ not at end of ‘class TYA::TMultiHist’ TH1F *fh[]; ^ ./include/TYAGlobalLibs.hh:102:9: note: in the definition of ‘class TYA::TMultiHist’ class TMultiHist{ ^~~~~~~~~~ It seems that this is a forbidden code path and as such it triggers a compiler error;

Accessing struct elements using single pointer?

泄露秘密 提交于 2021-01-28 19:51:06
问题 I have defined 5 floats within a struct: struct as {float a1, a2, a3, a4, a5;}; I'd like to access those 5 using a single pointer float* p_a = &a1; , and then just p_a++ , etc. Will it work? Converting to array float a[5] requires a big change I would like to avoid. 回答1: I would rather typedef struct { union { float fa[5]; struct { float f1,f2,f3,f4,f5; }; }; /* .... */ }MY_T; void foo() { MY_T s; float *p = s.fa; p++ *p = something; } 回答2: You can do ++ on array-element pointers and as long