unions

C# pinvoke structs with union and arrays

感情迁移 提交于 2019-12-10 09:43:32
问题 im failing to define the correct C# code to work with a C++ library which defines a complex struct with union and arrays, i keep getting some memory exception while executing the C++ code, and im pretty sure its due to this. The c++ struct is as follows, ignoring enums and other struct definition (i will post them if needed but they are pretty extense) typedef struct { DG_CCTALK_APP_EVT_CODE eEventCode; DG_CCTALK_APP_EVT_TYPE eEventType; int iTime; int iHandle; unsigned char uchAddress; DG

How to use Union in C language

岁酱吖の 提交于 2019-12-10 09:32:03
问题 I have a question about union in C Language. The variables declared in a union will share the same memory, ok, I understand. for example, union student { int i; int j; }x; how could we access the i and j? if we have: x.i = 1; and then we printf("%d",j); what will happen? compiler error? Ok then what about the following case: union student { int i; float j; }x; if we assign x.i = 2; what is the value of x.j? 回答1: Assuming you use printf("%d", x.j); You will see the same value you assigned to x

union versus void pointer

混江龙づ霸主 提交于 2019-12-10 01:28:54
问题 What would be the differences between using simply a void* as opposed to a union? Example: struct my_struct { short datatype; void *data; } struct my_struct { short datatype; union { char* c; int* i; long* l; }; }; Both of those can be used to accomplish the exact same thing, is it better to use the union or the void* though? 回答1: I had exactly the case in our library. We had a generic string mapping module that could use different sizes for the index, 8, 16 or 32 bit (for historic reasons).

Why does my union not show the correct values?

谁说我不能喝 提交于 2019-12-10 01:12:15
问题 union { int i; bool b; } x; x.i = 20000; x.b = true; cout << x.i; It prints out 19969. Why does it not print out 20000? 回答1: A union is not a struct . In a union , all of the data occupies the same space and can be treated as different types via its field names. When you assign true to x.b , you are overwriting the lower-order bits of 20000 . More specifically: 20000 in binary: 100111000100000 19969 in binary: 100111000000001 What happened here was that you put a one-byte value of 1 (00000001

get dictionary/object keys as tuple in typescript

99封情书 提交于 2019-12-09 20:44:53
问题 I would like to get proper tuple type with proper type literals from an object in TS 3.1: interface Person { name: string, age: number } // $ExpectType ['name','age'] type ObjectKeysTuple = ToTuple<keyof Person> Why?: To get proper string literals tuple when using Object.keys(dictionary) I wasn't able to find a solution for this as keyof widens to union which returns ('name' | 'age')[] which is definitely not what we want. type ObjectKeysTuple<T extends object> = [...Array<keyof T>] type Test

Getting Union, Intersection, or Difference of Sets in C++

守給你的承諾、 提交于 2019-12-09 17:10:05
问题 I have a couple questions about how to use C++ sets (std::set) Is there a way to get the union, intersection, or difference of two C++ sets? (It's pretty easy to write my own functionto do that but I wanted to know if there was a built in function for it) Can C++ sets be used as keys in a map? 回答1: Use the set_difference(), set_union(), set_intersection() and set_symmetric_difference() functions. Sets and maps support any key type that can compare. By default this means the type has operator<

std::optional implemented as union vs char[]/aligned_storage

时间秒杀一切 提交于 2019-12-09 09:46:24
问题 While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template <typename T> class optional { // ... private: bool has_value_; aligned_storage<T, /* ... */> storage_; } But then both libstdc++ and libc++ (and Abseil ) implement their optional types like this: template <typename T> class optional { // ... private: struct empty_byte {}; union { empty_byte empty_; T value_; }; bool has_value_; } They look to

How to check what type is currently used in union?

心已入冬 提交于 2019-12-09 04:19:52
问题 let's say we have a union: typedef union someunion { int a; double b; } myunion; Is it possible to check what type is in union after I set e.g. a=123? My approach is to add this union to some structure and set uniontype to 1 when it's int and 2 when it's double. typedef struct somestruct { int uniontype myunion numbers; } mystruct; Is there any better solution? 回答1: Is there any better solution? No, the solution that you showed is the best (and the only) one. union s are pretty simplistic -

Clarification on an example of unions in C11 standard

。_饼干妹妹 提交于 2019-12-09 02:51:40
问题 The following example is given in the C11 standard, 6.5.2.3 The following is not a valid fragment (because the union type is not visible within function f): struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *p1, struct t2 *p2) { if (p1->m < 0) p2->m = -p2->m; return p1->m; } int g() { union { struct t1 s1; struct t2 s2; } u; /* ... */ return f(&u.s1, &u.s2); } Why does it matter that the union type is visible to the function f? In reading through the relevant section a couple times,

How Do I Properly Declare a ctype Structure + Union in Python?

送分小仙女□ 提交于 2019-12-08 19:18:35
问题 I'm messing around with making a binary data parser, and while I could fall back on C, I wanted to see if I could use Python for the task. I have some inkling of how to get this going, and my current implementation looks something like this: from ctypes import * class sHeader(Structure): _fields_ = [("CC", c_uint8, 4), ("AFC", c_uint8, 2), ("TSC", c_uint8, 2), ("PID", c_uint16, 13), ("TP", c_uint16, 1), ("PSI", c_uint16, 1), ("TEI", c_uint16, 1), ("SyncByte", c_uint8)] class Header(Union):