unions

Is a Union Member's Destructor Called

心已入冬 提交于 2019-12-17 16:32:20
问题 C++11 allowed the use of standard layout types in a union : Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must manually destroy and construct when switching: http://en.cppreference.com/w/cpp/language/union#Explanation But what about an example like this: { union S { string str; vector<int> vec; ~S() {} } s = { "Hello, world"s }; } When s goes out of scope,

Is using an union in place of a cast well defined?

余生长醉 提交于 2019-12-17 12:47:35
问题 I had a discussion this morning with a colleague regarding the correctness of a "coding trick" to detect endianness. The trick was: bool is_big_endian() { union { int i; char c[sizeof(int)]; } foo; foo.i = 1; return (foo.c[0] == 1); } To me, it seems that this usage of an union is incorrect because setting one member of the union and reading another is not well-defined. But I have to admit that this is just a feeling and I lack actual proofs to strengthen my point. Is this trick correct ? Who

Anonymous union within struct not in c99?

烈酒焚心 提交于 2019-12-17 07:26:46
问题 here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node int_n; struct double_node double_n; }; }; int main(void) { struct int_node i; i.value = 10; struct node n; n.type = t_int; n. int_n = i; return 0; } And what I don't undestand is this: $ cc us.c $ cc -std=c99 us.c us.c:18:4: warning: declaration does not declare anything us.c: In

C++ union in C#

馋奶兔 提交于 2019-12-17 02:11:21
问题 I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct. What's the correct way of translating it into C#? And what does it do? It looks something like this; struct Foo { float bar; union { int killroy; float fubar; } as; } 回答1: You can use explicit field layouts for that: [StructLayout(LayoutKind.Explicit)] public struct SampleUnion { [FieldOffset(0)] public float bar; [FieldOffset(4)] public int killroy; [FieldOffset(4)] public float fubar; }

Unions and type-punning

拟墨画扇 提交于 2019-12-16 19:48:08
问题 I've been searching for a while, but can't find a clear answer. Lots of people say that using unions to type-pun is undefined and bad practice. Why is this? I can't see any reason why it would do anything undefined considering the memory you write the original information to isn't going to just change of its own accord (unless it goes out of scope on the stack, but that's not a union issue, that would be bad design). People quote the strict aliasing rule, but that seems to me to be like

C++ Union of a Float and a Byte Array Issue

不羁的心 提交于 2019-12-14 01:29:02
问题 I am working on an arduino (based off the AVR platform) and I have a method that takes in a float and writes it to EEPROM. I have to convert the float to a byte array to interact with EEPROM. I have two functions as follow: void WriteFloatToEEPROM(int address, float value) { union { byte byteVal[4]; float floatVal; } data; data.floatVal = value; for (int i = 0; i < 4; i++) { EEPROM.update(address + i, data.byteVal[i]); } } float ReadFloatFromEEPROM(int address) { union { byte byteVal[4];

Structure padding with union members of std::bitset

给你一囗甜甜゛ 提交于 2019-12-13 23:56:36
问题 After I had solved my issue to this question I went on to expand this version of my code to incorporate the unions of the data fields from my previous template versions with this version and I have this so far: main.cpp #include <iostream> #include <type_traits> #include "Register.h" int main() { using namespace vpc; std::cout << std::boolalpha; std::cout << "std::bitset<64> is trivially copyable " << std::is_trivially_copyable<std::bitset<64>>::value << '\n' << "QWord is trivially copyable "

Can I assign a value to one union member and read the same value from another?

我们两清 提交于 2019-12-13 19:40:31
问题 Basically, I have a struct foo { /* variable denoting active member of union */ enum whichmember w; union { struct some_struct my_struct; struct some_struct2 my_struct2; struct some_struct3 my_struct3; /* let's say that my_struct is the largest member */ }; }; main() { /*...*/ /* earlier in main, we get some struct foo d with an */ /* unknown union assignment; d.w is correct, however */ struct foo f; f.my_struct = d.my_struct; /* mystruct isn't necessarily the */ /* active member, but is the

Union float and int

泪湿孤枕 提交于 2019-12-13 15:36:37
问题 I'm little bit confused. During development of one function that based on predefined parameters, pass to sprintf function exact parameters needed based on their type, I found really strange behaviour ( something like "This is %f %d example", typeFloat, typeInt ). Please take a look at following stripped working code: struct Param { enum { typeInt, typeFloat } paramType; union { float f; int i; }; }; int _tmain(int argc, _TCHAR* argv[]) { Param p; p.paramType = Param::typeInt; p.i = -10; char

Bit fields for reading from H/W registers

社会主义新天地 提交于 2019-12-13 14:32:47
问题 I want to read the 2nd, 5th and the 6th bit from a 32-bit register. I have decided to use struct bit fields to store them. Is the following data structure correct? struct readData { int unwanted:1; int reqbit1:1; int unwanted1:2; int reqbit2:2; int unwanted2:26; }; I'm not sure about how the bit fields are created. I'm going to use an API that will copy bytes from the h/w register to this structure directly. In that case, will reqbit1 contain the 2nd bit? As per my understanding, the compiler