Converting a ulong to a byte array and constructing a ulong from it

welkam wwwelkam at gmail.com
Thu Oct 24 17:06:33 UTC 2019


On Thursday, 24 October 2019 at 14:08:36 UTC, 9898287 wrote:
> Does this contain any undefined behavior? It is in C as far as 
> I knew.

immutable int number = 1;
auto bad_idea = (cast(ubyte*) &number)[0 .. number.sizeof];
bad_idea[0] = 2;
writeln(number); //1
writeln(*(cast(int*)bad_idea.ptr)); //2

Cast is a powerful tool and with it you can destroy all type 
systems guarantees. In your case you will not have any problems 
just stay away from const and immutable


More information about the Digitalmars-d-learn mailing list