unions

union for uint32_t and uint8_t[4] undefined behavior? [duplicate]

江枫思渺然 提交于 2019-12-28 03:04:06
问题 This question already has answers here : Purpose of Unions in C and C++ (15 answers) Closed 5 years ago . In the comments of this answer it is said that it would be undefined behavior to split up an integer into their bytes using a union like follows. The code given at that place is similar though not identical to this, please give a note if have I changed undefined-behavior-relevant aspects of the code. union addr { uint8_t addr8[4]; uint32_t addr32; }; Up to now I thought this would be a

Access struct members as if they are a single array?

风流意气都作罢 提交于 2019-12-28 02:11:07
问题 I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call; } quantities; And then I use them to calculate: average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call; Every now and them I need to include another variable. Now, for

A question about union in C - store as one type and read as another - is it implementation defined?

纵然是瞬间 提交于 2019-12-27 11:14:34
问题 I was reading about union in C from K&R, as far as I understood, a single variable in union can hold any one of the several types and if something is stored as one type and extracted as another the result is purely implementation defined. Now please check this code snippet: #include<stdio.h> int main(void) { union a { int i; char ch[2]; }; union a u; u.ch[0] = 3; u.ch[1] = 2; printf("%d %d %d\n", u.ch[0], u.ch[1], u.i); return 0; } Output: 3 2 515 Here I am assigning values in the u.ch but

Single-function comparison of Union values

╄→гoц情女王★ 提交于 2019-12-25 05:38:31
问题 I have a union defined as follows: union V64 { double f64; __int64 i64; unsigned __int64 u64; }; I'd like to do a lazy comparison (equality and inequality) of a 8-byte value of unknown type against another V64 of known type. Will comparing the i64 of two V64 s consistently give me the expected result, regardless of the underlying type? For example: V64 a.u64 << 9007199254740993+500; //pseudo-code reading raw bytes V64 b.u64 << -9007199254740993-501; //pseudo-code reading raw bytes if(a.i64 >

C++ unions vs. reinterpret_cast

醉酒当歌 提交于 2019-12-25 03:11:42
问题 It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the integer value of 0xffff and literally interpret it as a series of bits in IEEE 754 floating point. (Binary convert shows visually how this is done.) #include <iostream> using namespace std; union unionType { int myInt; float myFloat; }; int main() { int

identifier int not a direct member of struct SOCKET_LOG_DATA

岁酱吖の 提交于 2019-12-24 15:43:02
问题 When I compile the below struct:- typedef PACKED struct PACKED_SUFFIX SOCKET_LOG_DATA { typedef PACKED union PACKED_SUFFIX { PACKED struct PACKED_SUFFIX { UINT16 loss_reason : 1; UINT16 unused : 15; } fields; UINT16 all_fields; } ; UINT16 socket_number; SOCKET_LOG_DATA () : all_fields(0), socket_number(0) {} } SOCKET_LOG_DATA; I get compilation error that:- error (dplus:1384): identifier all_fields not a direct member of SOCKET_LOG_DATA How do I fix this? 回答1: I fixed this with proper

Converting bit field to byte array

可紊 提交于 2019-12-24 11:35:48
问题 I have some mpeg ts bitfields, for example transport stream package: struct ts_package_header_s { unsigned int continuity_counter :4; unsigned int adaptation_field_control :2; unsigned int transport_scrambling_control :2; unsigned int PID :13; unsigned int transport_priority :1; unsigned int payload_unit_start_indicator :1; unsigned int transport_error_indicator :1; unsigned int sync_byte :8; }; struct ts_package_s { struct ts_package_header_s ts_header; unsigned char ts_body[TS_BODY]; };

is it legal to take the address of an union member in C?

丶灬走出姿态 提交于 2019-12-24 00:19:50
问题 I want to do something like this: union U { int i; double d; }; void foo (double *d) { *d = 3.4; } int main () { union U u; foo (&(u.d)); } gcc does not complain (with -Wall -Wextra ) and it works as expected, but I would like to make sure it is actually legal (according to the standard). 回答1: Why shouldn't it be legal? Actually, it's even guaranteed that the addresses of every member is equal to the address of whole union: A pointer to a union object, suitably converted, points to each of

C++ union assignment, is there a good way to do this?

一个人想着一个人 提交于 2019-12-23 21:14:40
问题 I am working on a project with a library and I must work with unions. Specifically I am working with SDL and the SDL_Event union. I need to make copies of the SDL_Events, and I could find no good information on overloading assignment operators with unions. Provided that I can overload the assignment operator, should I manually sift through the union members and copy the pertinent members or can I simply come some members (this seems dangerous to me), or maybe just use memcpy() (this seems

Bitfield and union for low level data structures and type conversion in Rust

大城市里の小女人 提交于 2019-12-23 20:13:49
问题 I need to manage bitfield data and unions. Here is the code like I think it in C: typedef struct __attribute__((__packed__)){ union { struct __attribute__((__packed__)){ unsigned short protocol : 4; unsigned short target : 12; unsigned short target_mode : 4; unsigned short source : 12; unsigned char cmd; unsigned char size; }; unsigned char unmap[6]; // Unmapped form. }; }header_t; I use this union to switch easily from a mapped to an unmapped form. I can write to header_t.protocol or header