unions

C union member gives particular/wrong value when other member is set to a new value. Why is this output in following code in C?

不打扰是莪最后的温柔 提交于 2019-12-23 17:27:57
问题 #include <stdio.h> int main() { union Data { char str[20]; int i; float f; }data; data.i=20; data.f=220.5; printf("%d\n",(data.i)); return 0; } The output is:1130135552. I used gcc complier on Ubuntu 16.04 LTS. Can someone explain the output? The members data.i and data.f occupy same memory location, so output should be 220. But why output is 1130135552? 回答1: According to C11 Section 6.5.2.3 Footnote 95 If the member used to read the contents of a union object is not the same as the member

Why does a struct, that has another struct wrapped in a union as a member not compile without an explicit default constructor?

房东的猫 提交于 2019-12-23 17:01:39
问题 This is the relationship I am talking about: struct A{ int i = 1; }; struct B{ union{A a;}; }; void main(){ B b; }; In this constellation, my compiler (vs2015) complains about the default constructor of B B::B(void) beeing deleted, with the note that the compiler has generated B::B : ../test.cpp(155): error C2280: "B::B(void)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen ../test.cpp(152): note: Compiler hat hier "B::B" generiert (sorry, I could not convince msvc to talk

memory alignment issues with union

社会主义新天地 提交于 2019-12-23 14:20:44
问题 Is there guarantee, that memory for this object will be properly aligned if we create object of this type in stack? union my_union { int value; char bytes[4]; }; If we create char bytes[4] in stack and then try to cast it to integer there might be alignment problem. We can avoid that problem by creating it in heap, however, is there such guarantee for union objects? Logically there should be, but I would like to confirm. Thanks. 回答1: Well, that depends on what you mean. If you mean: Will both

How does padding of structs inside unions work?

不想你离开。 提交于 2019-12-23 13:13:43
问题 I have the following: #include <stdio.h> typedef union u_data { struct { int a; int b; int c; }; int elem[3]; } my_data; int main(void) { my_data data; data.a = 3; data.b = 5; data.c = -3; printf("%d, %d, %d\n", data.elem[0], data.elem[1], data.elem[2]); } and it works as I expected with output: 3, 5, -3 however I understand that structs can have padding in them so does that mean that the elements in the struct might not always align with the array? 回答1: First of all, there is a special rule

c++ casting a union to one of its member types

狂风中的少年 提交于 2019-12-23 12:24:02
问题 The following seems perfectly logical to me, but isn't valid c++. A union cannot be implicitly cast to one of it's member types. Anyone know a good reason why not? union u { int i; char c; } function f(int i) { } int main() { u v; v.i = 6; f(v); } And can anyone suggest a clean alternative (the cleanest I can come up with is f(v.i); , which I admit is very clean, but the above just seems even cleaner) 回答1: While agreeing with Crazy Eddie that it doesn't look that better to me you can actually

ANSI C unions - are they really useful?

▼魔方 西西 提交于 2019-12-23 09:38:37
问题 From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlying alignment of the members. So after some research I found a written source that repeats this claim and specifies a popular example - using union of int and float to find the binary representation of a float. So, understanding that this assumption is not safe, I wonder - except for saving memory

… with constructor not allowed in union problem

你。 提交于 2019-12-23 08:18:32
问题 I desperately need to find a solution for the following problem: namespace test { template <int param = 0> struct Flags { int _flags; Flags() { _flags = 0; } Flags(int flags) { _flags = flags; } void init() { } }; union example { struct { union { struct { Flags<4096> f; }p1; //error: member 'test::example::<anonymous struct>::<anonymous union>::<anonymous struct> test::example::<anonymous struct>::<anonymous union>::p1' with constructor not allowed in union struct { Flags<16384> ff; }p2; /

Understanding the memory content of a union

这一生的挚爱 提交于 2019-12-23 07:54:04
问题 Suppose I define a union like this: #include <stdio.h> int main() { union u { int i; float f; }; union u tst; tst.f = 23.45; printf("%d\n", tst.i); return 0; } Can somebody tell me what the memory where tst is stored will look like? I am trying to understand the output 1102813594 that this program produces. 回答1: It depends on the implementation (compiler, OS, etc.) but you can use the debugger to actually see the memory contents if you want. For example, in my MSVC 2008: 0x00415748 9a 99 bb

Why does packing not work across sibling unions or structs

自闭症网瘾萝莉.ら 提交于 2019-12-23 03:03:44
问题 In the following example I expect the size of complex_t to be the same as uint16_t : 2 bytes, however it's 3 bytes. Removing the second union ("proximity_unsafe") reduces the size to 2 bytes, but I can't figure out the model of the packing rules. #include <stdint.h> #include <stdio.h> typedef union { uint16_t unsafe; struct { uint16_t backwardmotion_unsafe : 1; uint16_t batteryvoltage_unsafe : 1; union { uint16_t dropoff_unsafe : 4; struct { uint16_t dropofffrontleft_unsafe : 1; uint16_t

Member of Union has User-Defined Constructor

旧巷老猫 提交于 2019-12-23 02:24:07
问题 For the following code: class Foo{ int foo; public: Foo() : foo(13) {} int getFoo() const { return foo; } }; union Bar{ Foo fBar; double dBar; }; I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says: If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler And thus in gcc I can do this: Bar bar = { Foo() } When I try this in Visual Studio 2008 I get the error: error C2620: member Bar