unions

Union of same type in C++

我怕爱的太早我们不能终老 提交于 2019-12-01 17:27:16
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? No, this won't cause any issues. The reason you don't see it more often is that it's pointless - both names

Why is sizeof(std::variant) the same size as a struct with the same members?

廉价感情. 提交于 2019-12-01 16:54:17
问题 The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or it holds no value. sizeof(std::variant<float, int32_t, double>) == 16 But if it is a union, why does it take so much space? struct T1 { float a; int32_t b; double c; }; struct T2 { union { float a; int32_t b; double c; }; }; The variant has the same size as the struct sizeof(T1) == 16 sizeof(T2) == 8 I would expect the size of the

Why is my union's size bigger than I expected?

[亡魂溺海] 提交于 2019-12-01 16:29:05
When I print the size of a union like this: union u { char c[5]; int i; } un; using this: int _tmain(int argc, _TCHAR* argv[]) { printf("size of union = %d ",sizeof(un)); return 0; } I get an answer of 8 using Visual C++, but I expected 5. Why? Well, for the same example, i did something like this: int i1 = 0x98761234; un.i = i1; printf("\n un.c[0] = %x ",un.c[0]); printf("\n un.c[1] = %x ",un.c[1]); printf("\n un.c[2]= %x ",un.c[2]); printf("\n un.c[3] = %x ",un.c[3]); printf("\n un.c[4] = %x ",un.c[4]); printf("size of union = %d ",sizeof(un)); i got results like un.c[0] = 34; un.c[1] = 12;

Why is my union's size bigger than I expected?

﹥>﹥吖頭↗ 提交于 2019-12-01 15:27:23
问题 When I print the size of a union like this: union u { char c[5]; int i; } un; using this: int _tmain(int argc, _TCHAR* argv[]) { printf("size of union = %d ",sizeof(un)); return 0; } I get an answer of 8 using Visual C++, but I expected 5. Why? Well, for the same example, i did something like this: int i1 = 0x98761234; un.i = i1; printf("\n un.c[0] = %x ",un.c[0]); printf("\n un.c[1] = %x ",un.c[1]); printf("\n un.c[2]= %x ",un.c[2]); printf("\n un.c[3] = %x ",un.c[3]); printf("\n un.c[4] =

Do array elements count as a common initial sequence?

∥☆過路亽.° 提交于 2019-12-01 15:16:18
Sort of related to my previous question : Do elements of arrays count as a common initial sequence? struct arr4 { int arr[4]; }; struct arr2 { int arr[2]; }; union U { arr4 _arr4; arr2 _arr2; }; U u; u._arr4.arr[0] = 0; //write to active u._arr2.arr[0]; //read from inactive According to this cppreference page : In a standard-layout union with an active member of non-union class type T1, it is permitted to read a non-static data member m of another union member of non-union class type T2 provided m is part of the common initial sequence of T1 and T2.... Would this be legal, or would it also be

converting C to C#

萝らか妹 提交于 2019-12-01 14:43:43
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. Darin Dimitrov You may check out the following page . This being said, in .NET you have classes that allows you to work directly with sockets and TCP/IP such as Socket, TcpListener, TcpClient and you don't need to

Best practice for unions in Go

北慕城南 提交于 2019-12-01 14:00:51
问题 Go has no unions. But unions are necessary in many places. XML makes excessive use of unions or choice types. I tried to find out, which is the preferred way to work around the missing unions. As an example I tried to write Go code for the non terminal Misc in the XML standard which can be either a comment, a processing instruction or white space. Writing code for the three base types is quite simple. They map to character arrays and a struct. type Comment Chars type ProcessingInstruction

Union hack for endian testing and byte swapping

大憨熊 提交于 2019-12-01 09:39:21
For a union, writing to one member and reading from other member (except for char array) is UB. //snippet 1(testing for endianess): union { int i; char c[sizeof(int)]; } x; x.i = 1; // writing to i if(x.c[0] == 1) // reading from c[0] { printf("little-endian\n"); } else { printf("big-endian\n"); } //snippet 2(swap bytes using union): int swapbytes() { union // assuming 32bit, sizeof(int)==4 { int i; char c[sizeof(int)]; } x; x.i = 0x12345678; // writing to member i SWAP(x.ch[0],x.ch[3]); // writing to char array elements SWAP(x.ch[1],x.ch[2]); // writing to char array elements return x.i; //

Union hack for endian testing and byte swapping

时光毁灭记忆、已成空白 提交于 2019-12-01 08:14:00
问题 For a union, writing to one member and reading from other member (except for char array) is UB. //snippet 1(testing for endianess): union { int i; char c[sizeof(int)]; } x; x.i = 1; // writing to i if(x.c[0] == 1) // reading from c[0] { printf("little-endian\n"); } else { printf("big-endian\n"); } //snippet 2(swap bytes using union): int swapbytes() { union // assuming 32bit, sizeof(int)==4 { int i; char c[sizeof(int)]; } x; x.i = 0x12345678; // writing to member i SWAP(x.ch[0],x.ch[3]); //

Why aligning of long long union member is bigger than the containing union/struct? Is this correct?

自作多情 提交于 2019-12-01 03:42:54
From this question one could start to believe that alignment of a union is not less than the largest alignment of it's individual members. But I have a problem with the long long type in gcc/g++. The full example can be found here , but here are the relevant parts for my question: union ull { long long m; }; struct sll { long long m; }; int main() { #define pr(v) cout << #v ": " << (v) << endl pr(sizeof(long long)); pr(__alignof__(long long)); pr(sizeof(ull)); pr(__alignof__(ull)); pr(sizeof(sll)); pr(__alignof__(sll)); }; This results in the following output: sizeof(long long): 8 __alignof__