unions

Statically initialize anonymous union in C++

我与影子孤独终老i 提交于 2019-12-08 15:02:35
问题 I am trying to statically initialize the following structure in Visual Studio 2010: struct Data { int x; union { const Data* data; struct {int x; int y; }; }; }; The following is fails with error C2440: 'initializing' : cannot convert from 'Data *' to 'char' . static Data d1; static Data d = {1, &d1}; static Data d2 = {1, {1, 2}}; I have found references to some ways this can be initialized properly but none of them work in VS2010. Any ideas? 回答1: Can you do it by defining overloaded

Is const-casting via a union undefined behaviour?

北战南征 提交于 2019-12-08 14:42:28
问题 Unlike C++, C has no notion of a const_cast . That is, there is no valid way to convert a const-qualified pointer to an unqualified pointer: void const * p; void * q = p; // not good First off: Is this cast actually undefined behaviour? In any event, GCC warns about this. To make "clean" code that requires a const-cast (i.e. where I can guarantee that I won't mutate the contents, but all I have is a mutable pointer), I have seen the following "conversion" trick: typedef union constcaster_ {

Why does packing not work across sibling unions or structs

杀马特。学长 韩版系。学妹 提交于 2019-12-08 09:08:33
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 dropofffrontright_unsafe : 1; uint16_t dropoffsideleft_unsafe : 1; uint16_t dropoffsideright_unsafe : 1; }_

Questions about vector, union, and pointers in C++

一笑奈何 提交于 2019-12-08 04:55:15
问题 The questions I have are NOT homework questions, but I am considering using these concepts in my assignment. The context, if it helps, is like this: I need to keep track of several union instances and they belong to my own union in one of my own classes as class variables. (Note: the number of union instances is unknown, so I cannot just have a fixed number of union instances. Q1: If I have a union, say MyUnion, and many instances of this union, can I then put them into a vector like vector

C - Using an union, allocating memory

99封情书 提交于 2019-12-08 02:15:32
问题 I have a C structure that looks like this typedef struct event_queue{ Event* event; int size; int front; int count; int delay; } event_queue; It's a basic circular queue. The event value is an array of EventPointers, and it's traversed every X time to dequeue one of the events. It's initialized like p->event = calloc(p->size, sizeof(Event)); Thing is, I want to do a similar queue, with similar functionality, to queue other type of similar events but with slightly different data. Initially I

Using a union for multiple interpretations of IP address?

て烟熏妆下的殇ゞ 提交于 2019-12-07 18:06:59
问题 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? 回答1: According to the

Member of Union has User-Defined Constructor

落爺英雄遲暮 提交于 2019-12-07 17:11:30
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::fBar of union Bar has user-defined constructor or non-trivial default constructor Error C2620 states

access union member in c

孤人 提交于 2019-12-07 12:32:15
问题 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. }

c++11 unrestricted unions example

北慕城南 提交于 2019-12-07 10:09:00
问题 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

Why may there be a difference between union* and struct*?

孤街醉人 提交于 2019-12-07 05:06:35
问题 The C standard mandates that all pointers to union s have the same representation and alignment requirements. It mandates the same for all pointers to struct s. Thus my question: Why does the standard not mandate that pointers to union s have the same representation and alignment requirements as pointers to struct s? (I would very much appreciate an example of an implementation taking advantage of this.) Or did I simply miss the relevant text? The relevant quote from the draft standard n1570