unions

What is the point behind unions in C?

大憨熊 提交于 2020-01-02 02:39:08
问题 I'm going through O'Reilly's Practical C Programming book, and having read the K&R book on the C programming language, and I am really having trouble grasping the concept behind unions. They take the size of the largest data type that makes them up...and the most recently assigned one overwrites the rest...but why not just use / free memory as needed? The book mentions that it's used in communication, where you need to set flags of the same size; and on a googled website, that it can

generically convert from boost::variant<T> to type

本秂侑毒 提交于 2020-01-01 05:30:13
问题 I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I use to store different types of values in a struct. Only one of the specific type is ever going to be stored in that struct, however I have a vector of these structs which I need to go through and get the actual type out of the variant. Now when I need to do the conversion the types out of this variant I do this: variant second = mystruct.variant; if (second.which() == 5) /

Can we use va_arg with unions?

≡放荡痞女 提交于 2020-01-01 04:52:08
问题 6.7.2.1 paragraph 14 of my draft of the C99 standard has this to say about unions and pointers (emphasis, as always, added): The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa. All well and good, that means that it is legal

Union in Struct Error

六月ゝ 毕业季﹏ 提交于 2019-12-31 07:35:39
问题 I have the following struct: struct type1 { struct type2 *node; union element { struct type3 *e; int val; }; }; When initialising a pointer *f that points to an instance of type1 and doing something like: f.element->e or even just f.element , I get: error: request for member ‘element’ in something not a structure or union What am I overseeing here? 回答1: element is the name of the union, not the name of a member of type1 . You must give union element a name: struct type1 { struct type2 *node;

Naming Array Elements, or Struct And Array Within a Union

南楼画角 提交于 2019-12-30 17:21:53
问题 Consider the following struct: struct Vector4D { union { double components[4]; struct { double x, y, z, t; } Endpoint; }; }; It seems to me that I have seen something similar in WinApi's IPAddress struct. The idea is to give me the possibility to use the array components both by index and by name, for example: Vector4D v; v.components[2] = 3.0; ASSERT(v.Endpoint.z == 3.0) //let's ignore precision issues for now In the C++ standard there is a guarantee that there will be no "empty" space at

Define union that can access bits, nibbles, bytes

最后都变了- 提交于 2019-12-30 11:10:53
问题 union bits { unsigned int a : 1; unsigned int b : 2; unsigned int c : 3; unsigned int d : 4;`` unsigned char x[2]; unsigned int z; }; Suppose in a union of 32 bits, i need to use a single bit, or a group of bits, or nibble, or bytes. is there a way to define the union. 回答1: You need a union of bitfields. If you just use a union all of your fields will point to the same place. union{ struct { unsigned int bit1 : 1; unsigned int bit2 : 1; unsigned int bit3 : 1; unsigned int bit4 : 1; unsigned

Accessing C union members via pointers

拜拜、爱过 提交于 2019-12-30 04:23:13
问题 Does accessing union members via a pointer, as in the example below, result in undefined behavior in C99? The intent seems clear enough, but I know that there are some restrictions regarding aliasing and unions. union { int i; char c; } u; int *ip = &u.i; char *ic = &u.c; *ip = 0; *ic = 'a'; printf("%c\n", u.c); 回答1: It is unspecified (subtly different from undefined) behaviour to access a union by any element other than the one that was last written. That's detailed in C99 annex J: The

Common initial sequence in structures nested within union - definition in C standard

两盒软妹~` 提交于 2019-12-29 09:12:29
问题 In C11 standard there is following definition of common initial sequence shared by structures nested within a single union: 6.5.2.3/6 One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is

sizeof union larger than expected. how does type alignment take place here?

浪子不回头ぞ 提交于 2019-12-29 08:48:05
问题 #include <stdio.h> union u1 { struct { int *i; } s1; struct { int i, j; } s2; }; union u2 { struct { int *i, j; } s1; struct { int i, j; } s2; }; int main(void) { printf(" size of int: %zu\n", sizeof(int)); printf("size of int pointer: %zu\n", sizeof(int *)); printf(" size of union u1: %zu\n", sizeof(union u1)); printf(" size of union u2: %zu\n", sizeof(union u2)); return 0; } Results in: $ gcc -O -Wall -Wextra -pedantic -std=c99 -o test test.c $ ./test size of int: 4 size of int pointer: 8

Questions regarding C++ non-POD unions

自闭症网瘾萝莉.ら 提交于 2019-12-28 16:54:29
问题 C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active at a time, now my questions are rather simple. What is the default value of uny? - undefined? Whenever my class is destructed, which members (within the union), if any will be destructed? Suppose I have to std::typeinfo to keep track of which is the active member, should I then call the destructor