Convert some ints into a byte array without allocations?

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 16 11:45:13 PST 2016


On Saturday, 16 January 2016 at 14:46:47 UTC, Yazan D wrote:
> You can also use a union:
>
> union Foo
> {
>   int i;
>   ubyte[4] b;
> }
>
> // write to int part
> Foo f = Foo(a);
> // then read from ubyte part
> writeln(foo.b);
>
> ps. I am not sure of the aliasing rules in D for unions. In C, 
> this is allowed, but in C++, this is undefined behaviour AFAIK.

I sure hope it's not undefined behaviour in D, seeing as this 
technique is used several places in the standard library.


More information about the Digitalmars-d-learn mailing list