unions

What's the struct's initial sequence?

半城伤御伤魂 提交于 2019-12-10 17:12:10
问题 I came across the initial sequence concept. Serching through the Standard for initial sequence phrase gives only 3 results and they don't give a definition. Section N3797::9.5/1 [class.union] : If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct

Alignment of bitfields inside unions

∥☆過路亽.° 提交于 2019-12-10 16:49:17
问题 I'm a bit puzzled by how the following code gets layed out in memory: struct Thing { union { unsigned value:24; uint8_t bytes[3]; }; Thing(int v) :value(v) {} void foo() { printf("Thing %p value=%d !\n", this, value); } } __attribute__((__packed__)); On gcc 3.3, 4.3 or 4.6 on Linux (without any special options I can think of - only "-Wall -g" on 4.6), the size of the structure is always 4: $ pahole ./union struct Thing { union { unsigned int value; /* 4 */ unsigned char bytes[3]; /* 3 */ }; [

Do scalar members in a union count towards the common initial sequence?

不想你离开。 提交于 2019-12-10 15:57:14
问题 In the union U below, if a or b is the active member, is it defined behavior to access c ? struct A{ int a; }; struct B{ int a; double b; }; union U{ A a; B b; int c; }; In [class.union], the standard defines some rules to make using a union easier ( emphasis mine): [ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence, and if a non-static data member of an object

4th bullet point in iso 12.1 p5 doesn't make sense to me

◇◆丶佛笑我妖孽 提交于 2019-12-10 15:18:02
问题 Maybe I'm missing something, but IMO the 4th bullet point in iso §12.1 p5 is wrong: X is a union and all of its variant members are of const-qualified type (or array thereof), simply because you can't have more than one const qualified member in a union. From §9.1 we have: In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time. Edit: This snippet doesn't compile in

hive querying records for a specific uniontype

女生的网名这么多〃 提交于 2019-12-10 13:46:40
问题 I have a sample hive table created as CREATE TABLE union_test(foo UNIONTYPE<int, double, array<string>, struct<a:int,b:string>>); The data can be viewed as SELECT foo FROM union_test; The output is {0:1} {1:2.0} {2:["three","four"]} {3:{"a":5,"b":"five"}} {2:["six","seven"]} {3:{"a":8,"b":"eight"}} {0:9} {1:10.0} the first field (tag) denotes the type of the union ( 0 for int, 1 for double, 2 for array etc). My problem is if I found to select only those records where the union type is 2

Unions and strict aliasing in C11

旧时模样 提交于 2019-12-10 13:29:57
问题 Assuming I have a union like this union buffer { struct { T* data; int count; int capacity; }; struct { void* data; int count; int capacity; } __type_erased; }; Will I get into trouble if I mix reads/writes to the anonymous struct members and __type_erased members under C11 aliasing rules? More specifically, I am interested in the behaviour that occurs if the components are accessed independently (e.g. via different pointers). To illustrate: grow_buffer(&buffer.__type_erased); buffer.data

Objective-C KVO doesn't work with C unions

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:29:33
问题 I need to observe union-typed properties on an Objective-C class using KVO, but it seems I have no luck with this. I did some experiments: everything works fine as long as I am using a C struct. As soon as I replace the struct with a union, automatic KVO doesn't work anymore ( observeValueForKeyPath is not being called). Here's my small test class: AppDelegate.h: #import <Cocoa/Cocoa.h> typedef union { float data[3]; struct { float x,y,z; }; } vec3union; typedef struct { float x,y,z; }

C++11 empty list Initialization of a union - is it guaranteed to initialize the full length of the union?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:49:02
问题 In C++11, I have the following union: union SomeData { std::uint8_t Byte; std::uint16_t Word; std::uint32_t DWord; unsigned char String[128]; }; If I initialize the union thusly; SomeData data {}; Is it guaranteed that the entire contents of the union will be "zero'd" out? Put another way; is an empty list-initializer of a union functionally equivalent to memset-ing the union to Zero?: memset(&data, 0, sizeof(data)); In particular, I'm concerned about the string data. I'd like to ensure the

Initialize union using largest member under MSVC compiler

流过昼夜 提交于 2019-12-10 10:59:00
问题 I'm trying to initialize a LARGE_INTEGER to 0 in a C++ library (C++03 to be exact). Previously, the initialization was: static LARGE_INTEGER freq = { 0 }; Under MinGW it produced a warning: missing initializer for member '_LARGE_INTEGER::::HighPart' So I changed the initialization to the following in accordance with Can a union be initialized in the declaration?: static LARGE_INTEGER freq = { .QuadPart = 0 }; I'm now testing under Visual Studio 2015, and its producing an error: 81 static

How to pack a struct in Visual Studio to 24 bits that contains an uint32_t?

孤街浪徒 提交于 2019-12-10 10:53:07
问题 I am trying to port over an existing application from a 32-Bit ARM-microcontroller to desktop plattforms such as Microsoft Windows. GCC is used on the ARM and I was able successfully compile the application on windows using a 32-bit MinGW-compiler, however I had no success using Microsoft's Visual Studio Compiler and that is the reason why I am asking here for help. Here is what my application is doing: I have some framebuffer consisting of three bytes per pixel, so my memory looks like