[haiku-commits] Re: haiku: hrev52133 - src/apps/mediaconverter
- From: looncraz <looncraz@xxxxxxxxxxxx>
- To: haiku-commits@xxxxxxxxxxxxx
- Date: Thu, 26 Jul 2018 13:37:25 -0500
On 7/26/2018 11:22, looncraz wrote:
Those structs MUST occupy the same memory inside the union. The
largest struct will determine the allocation size - so sizeof(B) in
this case.
You only need to reset all values (including padding) inside the
largest struct to zero and everything else will be initialized to zero
since it occupies the same memory, though I never do it that way since
it requires drilling down using an instance of the object
I just pad the union with a member external to the deeper types and
set the padding to zero... (I don't like memset - it's sets everything
a byte at a time). I always use uint64 for this (or unsigned long long).
Disregard that - just ran a test with it, and B::c does, in fact, not
get initialized despite setting the padding much larger than either
struct. What an odd quirk considering what unions are supposed to do
(place all members in the same memory).
The largest struct will need an Unset() method that is invoked by the
Unset() method of the union. This works perfectly.
union SomeUnion {
SomeUnion() {
Unset();
}
void Unset() {
_B.Unset();
}
struct A {
int a;
int b;
};
struct B {
int a;
int b;
int c;
void Unset() {
a = 0;
b = 0;
c = 0;
}
};
A _A;
B _B;
};
Other related posts: