unions

Bitfields in C with struct containing union of structs

非 Y 不嫁゛ 提交于 2019-12-06 08:33:29
问题 Hm... why is it that, when I print sizeof(struct MyStruct) , it outputs 3 (instead of 2) for this code? #pragma pack(push, 1) struct MyStruct { unsigned char a : 6; union { struct { unsigned int b : 9; }; }; }; #pragma pack(pop) In case it matters, I'm running MinGW GCC 4.5.0 on Windows 7 x64, but honestly, the result is weird enough for me that I don't think the compiler and the OS matter too much here. :\ 回答1: You can't have the field starting at an address that is not byte aligned. You're

Actual usage of union in C [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-06 07:49:34
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C: Where is union practically used? I know the concept of union but I don't see the actual case in real world coding that I should use union data structure. I will be very appreciated if you guys can give me some examples or tutorials that shows cases using union properly. Thanks in advance. 回答1: Where I use it constantly: parsing a configuration file I store all values in a union data type. E.g. when values can

Why can't anonymous unions contain members with non-trivial constructors/destructors?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 02:24:36
问题 I may be mistaken, but the basic explanation I've found has been that the union can't initialize because it doesn't know which member's constructor to call. The compiler can not automatically generate a constructor for the union. Why is the user not allowed to define the unions constructor? This would remove said issue and allow the presence of union members that have a non-trivial constructor/destructor. Also, why can't a union member have any custom constructors? The previous explanation

Using a union for multiple interpretations of IP address?

早过忘川 提交于 2019-12-06 00:00:56
At work we have the following construct to enable interpreting an IP address as either an array of 4 bytes, or as a 32-bit integer: union IPv4 { std::uint32_t ip; std::uint8_t data[4]; }; This works fine, but I'm a little worried after reading chapter 97 "Don't use unions to reinterpret representation" of the book C++ coding standards . The example in the book is more insidious though and I'm not sure if it applies to my code. Are there any potential issues with my code? According to the standard, reading a member of a union other than the last one written is undefined behavior. Unions were

What's the correct way of using bitfields in C?

試著忘記壹切 提交于 2019-12-05 23:13:47
问题 I am using bitfields to get easy access on a float library I am trying to make for a microcontroller with no FPU. The problem is that I can't seem to make it work with bitfields. Take a look: typedef struct { union{ unsigned long mantissa: 23; unsigned long exponent: 8; unsigned long sign: 1; float all; }; }_float __attribute__((__packed__)); The problem is that when I try to access or change anything it considers the bitfields as 1,8,23 bits from the end respectively. While it should be 23

Is the following C union access pattern undefined behavior?

天涯浪子 提交于 2019-12-05 22:18:51
问题 The following is not undefined behavior in modern C: union foo { int i; float f; }; union foo bar; bar.f = 1.0f; printf("%08x\n", bar.i); and prints the hex representation of 1.0f. However the following is undefined behavior: int x; printf("%08x\n", x); What about this? union xyzzy { char c; int i; }; union xyzzy plugh; This ought to be undefined behavior since no member of plugh has been written. printf("%08x\n", plugh.i); But what about this. Is this undefined behavior or not? plugh.c = 'A'

C# pinvoke structs with union and arrays

…衆ロ難τιáo~ 提交于 2019-12-05 20:53:35
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_CCTALK_BILL_INFO sBillInfo; int iBillAmount; union { long lData; int iData; unsigned char ucArray[128];

access union member in c

江枫思渺然 提交于 2019-12-05 19:53:28
I have a question about union in c language for example: typedef struct { int a; float c; }Type1; typedef struct { int b; char d; }Type2; union Select { Type1 type1; Type2 type2; }; void main() { Select* select; //can we access type1 first and then access type2 immediately? like this way: select->type1.a; select->type2.b; //after access type1, and then access type2 immediately, can we get the value b of type2? //I modify the first post a little bit, because it is meanless at the beginning. } Yes, that is correct. In your example (ignoring the uninitialised pointer) the value of type1.a and

c++11 unrestricted unions example

走远了吗. 提交于 2019-12-05 17:42:42
I read http://www.stroustrup.com/C++11FAQ.html#unions but I can't compile the given example: union U1 { int m1; complex<double> m2; // ok }; union U2 { int m1; string m3; // ok }; U1 u; // ok u.m2 = {1,2}; // ok: assign to the complex member Results in: main.cpp:85:8: error: use of deleted function 'U1::U1()' U1 u; // ok ^ main.cpp:75:11: note: 'U1::U1()' is implicitly deleted because the default definition would be ill-formed: union U1 { ^ main.cpp:77:25: error: union member 'U1::m2' with non-trivial 'constexpr std::complex<double>::complex(double, double)' complex<double> m2; // ok ^ main

Union of layout-compatible types

匆匆过客 提交于 2019-12-05 16:34:30
问题 Look at this code: struct A { short s; int i; }; struct B { short s; int i; }; union U { A a; B b; }; int fn() { U u; u.a.i = 1; return u.b.i; } Is it guaranteed that fn() returns 1 ? Note: this is a follow-up question to this. 回答1: Yes, this is defined behavior. First lets see what the standard has to say about A and B . [class.prop]/3 has A class S is a standard-layout class if it: has no non-static data members of type non-standard-layout class (or array of such types) or reference, has no