unions

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

孤街浪徒 提交于 2019-12-04 04:46:55
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 bits from the end, then 8 bits and then the last bit. Unless I have totally misunderstood the use of

union initialisation

狂风中的少年 提交于 2019-12-04 04:11:46
I'm attempting to globally initialise a union as in the following example: #include <cstdio> typedef union { char t[4]; int i; } a; enum { w = 5000, x, y, z }; a temp = {w}; int main() { printf("%d %d %d %d %d\n", temp.t[0],temp.t[1],temp.t[2],temp.t[3],temp.i); return 0; } However, if you run the code, you'll note that neither of temp.i or temp.t[...] actually give the correct item i initialised the union with. I'd imagine this would be avoided if I could manually initialise the integer member, but unfortunately I can't. I also can't change the ordering of elements within the struct (swapping

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

左心房为你撑大大i 提交于 2019-12-04 03:54:45
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; [FieldOffset(0)] public STRUCT2 Struct2; } [StructLayout(LayoutKind.Sequential)] internal struct MASTER

Do array elements count as a common initial sequence?

跟風遠走 提交于 2019-12-04 02:52:49
问题 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

Union of layout-compatible types

佐手、 提交于 2019-12-04 01:27:44
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 . NathanOliver- Reinstate Monica 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 virtual functions and no virtual base classes, has the same access control

Is there an elegant way to swap references in C++?

大城市里の小女人 提交于 2019-12-04 00:27:51
问题 Sometimes classes are referencing other classes. Implementing std::swap() for such classes cannot be straightforward, because it would lead to swapping of original instances instead of references. The code below illustrates this behavior: #include <iostream> class A { int& r_; public: A(int& v) : r_(v) {} void swap(A& a) { std::swap(r_, a.r_); } }; void test() { int x = 10; int y = 20; A a(x), b(y); a.swap(b); std::cout << "x=" << x << "\n" << "y=" << y << "\n"; } int main() { test(); return

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

坚强是说给别人听的谎言 提交于 2019-12-03 23:36:39
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? Shafik Yaghmour 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, int); void g(int i, int* v) { i = v[i++]; // the behavior is undefined i = 7, i++, i++; // i

Typescript has unions, so are enums redundant?

China☆狼群 提交于 2019-12-03 22:04:02
Ever since TypeScript introduced unions types, I wonder if there is any reason to declare an enum type. Consider the following enum type declaration: enum X { A, B, C } var x:X = X.A; and a similar union type declaration: type X: "A" | "B" | "C" var x:X = "A"; If they basically serve the same purpose, and unions are more powerful and expressive, then why are enums necessary? Amid As far as I see they are not redundant, due to the very simple reason that union types are purely a compile time concept whereas enums are actually transpiled and end up in the resulting javascript ( sample ). This

An union with a const and a nonconst member?

橙三吉。 提交于 2019-12-03 19:45:49
问题 This appears to be undefined behavior union A { int const x; float y; }; A a = { 0 }; a.y = 1; The spec says Creating a new object at the storage location that a const object with static, thread, or automatic storage duration occupies or, at the storage location that such a const object used to occupy before its lifetime ended results in undefined behavior. But no compiler warns me while it's an easy to diagnose mistake. Am I misinterpreting the wording? 回答1: The latest C++0x draft standard

Is it possible to put several objects together inside a union?

℡╲_俬逩灬. 提交于 2019-12-03 17:11:33
问题 What if I have this: union{ vector<int> intVec ; vector<float> floatVec ; vector<double> doubleVec ; } ; Of course, I'll be using just one of the 3 vectors. But... what happens when all the 3 vectors are contructed?? Would the consructors of the 3 vectors interfere with each other?? (since the 3 of them are in the same memory address) Thanks. 回答1: Current C++ standard does not allow non-POD types inside unions. You will get this compiler error from gcc : error: member ‘std::vector<int, std: