unions

Unions in C#: Structure Members Do Not Seem to be Aligned

强颜欢笑 提交于 2019-12-21 09:07:12
问题 I have defined to following structures to emulate a C++ union (which will eventually be used for C++ Interop): [StructLayout(LayoutKind.Sequential)] internal struct STRUCT1 { public Guid guid; public String str1; public String str2; } [StructLayout(LayoutKind.Sequential)] internal struct STRUCT2 { public Guid guid; public String str1; public String str2; public Int32 i1; } [StructLayout(LayoutKind.Explicit)] internal struct MASTER_STRUCT_UNION { [FieldOffset(0)] public STRUCT1 Struct1;

Union common initial sequence with primitive

荒凉一梦 提交于 2019-12-21 07:17:13
问题 I am trying to better understand a rather surprising discovery regarding unions and the common initial sequence rule. The common initial sequence rule says (class.mem 23):  In a standard-layout union with an active member of struct type T1, it is permitted to read a non-static data member m of another union member of struct type T2 provided m is part of the common initial sequence of T1 and T2; the behavior is as if the corresponding member of T1 were nominated. So, given: struct A { int a;

Can you assign the value of one union member to another?

£可爱£侵袭症+ 提交于 2019-12-21 07:13:10
问题 Consider the following code snippet: union { int a; float b; }; a = /* ... */; b = a; // is this UB? b = b + something; Is the assignment of one union member to another valid? 回答1: Unfortunately I believe the answer to this question is that this operation on unions is under specified in C++, although self assignment is perfectly ok. Self assignment is well defined behavior, if we look at the draft C++ standard section 1.9 Program execution paragraph 15 has the following examples: void f(int,

select an union member depending on a template parameter

不羁的心 提交于 2019-12-20 10:47:28
问题 I'm dealing with an union in C++, and I would like have a function template which access to the active union member depending on a template parameter. Code is something like (doSomething is just an example): union Union { int16_t i16; int32_t i32; }; enum class ActiveMember { I16 , I32 } template <ActiveMember M> void doSomething(Union a, const Union b) { selectMemeber(a, M) = selectMember(b, M); // this would be exactly (not equivalent) the same // that a.X = b.X depending on T. } To

Union of same type in C++

人走茶凉 提交于 2019-12-19 18:54:28
问题 Whenever I see examples of union, they are always different types. For example, from MSDN: // declaring_a_union.cpp union DATATYPE // Declare union type { char ch; int i; long l; float f; double d; } var1; // Optional declaration of union variable int main() { } What happens if I have a union (in this case anonymous, but that shouldn't matter) like this: union { float m_1stVar; float m_1stVarAlternateName; }; Regardless of whether this is good practice or not, will this cause any issues? 回答1:

converting C to C#

ⅰ亾dé卋堺 提交于 2019-12-19 12:05:04
问题 I'm trying to convert this C code to C#, is there a C# equivalent to the C union typedef? struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; typedef struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; } IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR; Thanks. 回答1: You may check out the following page. This being said, in .NET you have classes that allows you to work directly

Active member of an union, uniform initialization and constructors

醉酒当歌 提交于 2019-12-19 07:23:27
问题 As the (Working Draft of) C++ Standard says: 9.5.1 [class.union] In a union, at most one of the non-static data members can be active at any time , that is, the value of at most one of the non-static data members can be stored in a union at any time. [...] The size of a union is sufficient to contain the largest of its non-static data members. Each non-static data member is allocated as if it were the sole member of a struct. All non-static data members of a union object have the same address

Anonymous union and struct [duplicate]

跟風遠走 提交于 2019-12-19 05:14:50
问题 This question already has answers here : Why does C++ disallow anonymous structs? (6 answers) Closed 5 years ago . How would you go about doing this in standard C++11/14 ? Because if I'm not mistaken this isn't standard compliant code with the anonymous structs. I wish to access the members the same way as you would with this. template <typename some_type> struct vec { union { struct { some_type x, y, z; }; struct { some_type r, g, b; }; some_type elements[3]; }; }; 回答1: Yes, neither C++11

What is the equivalent of boost::variant in the C++ standard library?

三世轮回 提交于 2019-12-19 05:14:17
问题 I am looking for an alternative to C-style union. boost::variant is one such option. Is there anything in std C++ ? union { int i; double d; } 回答1: As several commenters said: No, there is no Boost Variant-alike in standard C++. Maybe in a few years there will be, but why wait--use Boost Variant today! Edit (four years later, 2016): In C++17 there will be std::variant . Similar but not identical to boost::variant . So when your compiler supports C++17, you will have a solution in the standard

Why this union's size is 2 with bitfields?

风流意气都作罢 提交于 2019-12-19 04:07:10
问题 I am working on turbo C on windows where char takes one byte.Now my problem is with the below union. union a { unsigned char c:2; }b; void main() { printf("%d",sizeof(b)); \\or even sizeof(union a) } This program is printing output as 2 where as union should be taking only 1 byte. Why is it so? for struct it is fine giving 1 byte but this union is working inappropriately. And one more thing how to access these bit fields. scanf("%d",&b.c); //even scanf("%x",b.c); is not working because we