unions

Memory position of elements in C/C++ union

烈酒焚心 提交于 2019-11-30 08:26:45
I have a union in C like this: union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp_num; }; My question is, can I guarantee that if given the following: union AUnion u; Then the following are true: &u == &u.num &u == &u.fp_num &u == &u.charbuf I.e they all start at the beginning of the memory segment where u is stored. In the case of this C program compiled with gcc version 5.3.0 and -std=c11 the above is true: #include <stdio.h> #include <stdint.h> #include <stdlib.h> union AUnion { struct CharBuf { char *buf; size_t len; } charbuf; uint8_t num; double fp

Memory layout of union of different sized member?

◇◆丶佛笑我妖孽 提交于 2019-11-30 08:22:48
typedef union epoll_data { void *ptr; int fd; __uint32_t u32; __uint64_t u64; } epoll_data_t; Here int and __uint32_t are 4 bytes,while the others are 8 bytes. When we set fd to an int ,does it lie on the first 4 bytes or the last 4 bytes,or does it depend on endianness? Some reason is appreciated. It lies on the first 4 bytes. From the C99 standard §6.7.2.1/14: 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

Is a union in C++ actually a class?

好久不见. 提交于 2019-11-30 08:12:22
A junior developer asked me if it was possible to overload assignment operators for a union with POD arguments such that the corresponding data type within the union would get written to when an instance of the union is assigned to variable of that type. I replied that I did not think so, but then played around with the following code. To my surprise this code actually compiled (using g++ version 4.6.3 on Ubuntu 12.04) union unMember { float fData; unsigned int uiData; unMember():uiData(0) {}; unMember(float data):fData(data) {}; unMember(unsigned int data):uiData(data) {}; operator float()

C++: unions with methods?

旧城冷巷雨未停 提交于 2019-11-30 08:11:18
Is there anything wrong with a union having one or more methods? Or anything to watch out for? (I can see constructors/destructors being problematic for schizophrenic reasons) From the C++03 & C++0x (Draft N3092) standards: 9.5 Unions A union can have member functions (including constructors and destructors) , but not virtual (10.3) functions . A union shall not have base classes. A union shall not be used as a base class. Initializing the union using the aggregate initializer syntax ( U u = { 42 }; ) or setting a member afterwards ( U u; u.i = 42; ) is not "problematic". And neither is

memcpy/memmove to a union member, does this set the 'active' member?

折月煮酒 提交于 2019-11-30 01:40:04
Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy , it copies from the address of a plain old uint32_t , which is not contained within a union. Also, I am copying (via memcpy ) to a specific member of a union ( u.a16 or &u.x_in_a_union , not directly to the entire union itself ( &u ) C++ is quite strict about unions - you should read from a member only if that was the last member that was written to: 9.5 Unions [class.union] [[c++11]] In a union, at most one of the non-static data members can be active at any time, that is, the

When to use a union and when to use a structure

泪湿孤枕 提交于 2019-11-29 21:45:26
问题 I know the differences between union and structure. But from a design and coding perspective what are the various use cases of using a union instead of a structure? One is a space optimization. Are there any more advantages of using them? 回答1: There's really only two major uses. The first is to create a discriminated union. That's probably what you were thinking of by "space optimization," but there's a bit more to it. You need an extra bit of data to know which member of the union is "alive"

Union and struct packing problem

风格不统一 提交于 2019-11-29 20:13:10
问题 I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important. typedef union{ uint32_t raw; struct{ unsigned int present:1; unsigned int rw:1; unsigned int user:1; unsigned int dirty:1; unsigned int free:7; unsigned int frame:20; } __packed; }__packed page_union_t; that is my structure and union. It does not work however: page_union_t p; //..... //This: p.frame=trg_page; p.user=user; p.rw=rw; p.present=present; //and this: p.raw=trg_page<<12 | user<<2

How to cast C struct just another struct type if their memory size are equal?

风流意气都作罢 提交于 2019-11-29 18:12:47
问题 I have 2 matrix structs means equal data but have different form like these: // Matrix type 1. typedef float Scalar; typedef struct { Scalar e[4]; } Vector; typedef struct { Vector e[4]; } Matrix; // Matrix type 2 (you may know this if you're iPhone developer) // Defines CGFloat as float for simple description. typedef float CGFloat; struct CATransform3D { CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m24; CGFloat m31, m32, m33, m34; CGFloat m41, m42, m43, m44; }; typedef struct

Constructor and copy-constructor for class containing union with non-trivial members

醉酒当歌 提交于 2019-11-29 18:04:27
问题 I am trying to implement a custom variant type which uses a union to store data of various different types. In the field type_id I plan to store which type the data stored in the union is of. The union contains non-trivial members. Here is my current implementation: struct MyVariant { enum { t_invalid, t_string, t_int, t_double, t_ptr, t_dictionary } type_id; union { int as_int; double as_double; std::string as_string; std::unique_ptr<int> as_ptr; std::map<int, double> as_dictionary; }; }; I

Copying (using assignment) a structure to a structure inside a union causing seg fault

依然范特西╮ 提交于 2019-11-29 16:47:51
I wrote the following code: #include <iostream> #include <string> #include <cstring> struct bar { std::string s3; std::string s4; }Bar; union foo { char * s1; char * s2; bar b1; foo(){}; ~foo(){}; }Foo; int main () { foo f1; bar b2; std::string temp("s3"); b2.s3 = temp; b2.s4 = temp; //f1.b1 = b2; //-- This Fails (Seg faults) /* #0 0x00002b9fede74d25 in std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.12] () from /usr/local/lib64/libstdc++.so.6 #1 0x00002b9fede75f09 in std::string::assign(std::string const&) () from /usr/local/lib64/libstdc++.so.6 #2 0x0000000000400ed1