Questions tagged [unions]
The plural of a keyword in the C family of languages for declaring a union data type.
unions
1,695
questions
3
votes
1
answer
80
views
Can you call virtual methods on a base class in a union when a derived class is active?
While std::variant is great for some use cases, it's a bit analogous to std::tuple in that you can't name each individual variant. Often a struct is better than a tuple.
I'm wondering if it's legal ...
0
votes
0
answers
32
views
STM32 HAL_TIM_PWM_PulseFinishedCallback not being called. Suspect that problem is related to use of struct or union pointers
I'm writing a Neopixel driver for STM32 based on the example provided here.
In summary, the pixels aren't lighting correctly. My latest attempt had the first row of 8 pixels light up green before it ...
1
vote
1
answer
57
views
is accessing the same type through a struct member in a union undefined? [duplicate]
I want to know whether the standard (note not the compilers) say if this is undefined
struct Vec2{int x,y;};
union {
Vec2 v;
int x;
} u = {{0,0}}; // v is set
u.v.x; // this is defined
u.x;...
2
votes
1
answer
99
views
Is There a Pragmatic Solution to Remove Enum Switches?
The title of this question is a little strange-sounding, but I could think of no better way to word it. My problem is this; I have a type inside a project called AmbiguousType, which is a union in ...
0
votes
1
answer
87
views
Union between an array and a struct of arrays. Can memory layout be tested at comptime?
While experimenting with the C language, I realized that I could "name" my matrix dimensions using a union.
typedef union {
float a[2][10];
struct {
float a_0[10];
...
0
votes
1
answer
111
views
C program structure and union bitfields
typedef union
{
struct
{
uint8_t ctr;
uint8_t write_ptr;
uint8_t read_ptr;
unsigned pause:1; // To pause updating of arrays by the ISR
...
1
vote
1
answer
98
views
C++ Unions: Accessing non-active array element guaranteed to not share space
Consider the following C++ union:
struct Mystruct
{
char *p; // sizeof(p) == 8, alignof(p) == 8
uint32_t sz; // sizeof(sz) == 4, alignof(sz) == 4
}; // sizeof(MyStruct) ...
0
votes
1
answer
69
views
Ignoring type in Fail method in standard C# result class
I've worked with Result classes for many years and when I was exposed to C# the first encounter I had was with John Ottosson result class. This is the version of the result class I used as a basis for ...
4
votes
1
answer
144
views
Can you overwrite the memory of a union, if no members are active?
The following program sets the table, eats, and clears the table. The place where the dish goes is cleaned before and after eating. Is that allowed according to the C++23 standard? Or is it undefined ...
0
votes
0
answers
70
views
How to handle endianness in variable length payloads with named fields?
I am implementing a custom communication protocol which uses payloads of variable lengths. I need to access both the raw bytes of the payload as well as named fields which correspond to specific bits/...
1
vote
1
answer
63
views
Undefined behavior with unions
While working with 3D points, I came across this approach to type definition :
union point_3d {
struct {
GLdouble x, y, z;
} coord;
GLdouble tab[ 3 ];
};
Thus, the coordinates ...
1
vote
1
answer
71
views
Using unions with arrays of non-trivial types
If I have a union that contains an array of a non-trivial type (for example std::string)
using namespace std;
struct MyUnion {
union {
char c;
string s[5];
};
MyUnion() {}
...
0
votes
2
answers
69
views
Using a undefined structure is not throwing an error
I have a code snippet like -
typedef struct {
int data;
struct abc *nxt; // Have not defined the structure of name "abc"
} mynode_t;
Here I have not defined a structure ...
2
votes
1
answer
70
views
eBPF program fails to attach because of null pointer exception
I have an eBPF programs that currently fails to attach to trace point syscalls/sys_exit_mmap; ebpf.NewCollectionWithOptions() throws an error: "invalid memory address or nil pointer dereference [...
0
votes
0
answers
61
views
Union structs with uint16_t and two uint8_t variables results in 3 byte long allocation?
I have a memory structure that contains various structures and variables defined as below with CRCr as UNION struct that defines uint16_t and two uint8_t variables. Later struct containing bits and ...