Is it safe in D to cast pointers to structures like this?

John Burton john.burton at jbmail.com
Tue Jan 14 12:05:01 UTC 2020


After years of C++ I have become paranoid about any casting of 
pointers being undefined behavior due to aliasing so want to see 
if :-

1) This is safe to do in D.
2) If not is there anything I can do to make it safe.
3) If not, what is the best approach?

I have a void* pointing to a block of allocated memory. In that 
memory I have a header struct at the start, and some of the 
members of that struct are offsets into the memory of other 
structs.

Can I do this? It appears to compile and "work" in dmd 64 bit but 
I need to make sure it's valid and reliable code. (This is a 
minimal example without any error checking etc)

import std.stdio;

//
// getMemory is just an example to make this compile...
//
void* getMemory()
{
     static byte[100] someData;
     // Something fills in the data here
}

struct Header
{
     ulong data1;
     ulong data2;
}

struct Data1
{
     int a;
}

struct Data2
{
     int b;
     float[10] d;
}

void main()
{
     void* memory = getMemory();
     auto header = cast(Header*)memory;
     auto data1 = cast(Data1*)(memory + header.data1);
     auto data2 = cast(Data2*)(memory + header.data2);

     writeln(data1.a, " ", data2.b);

}


More information about the Digitalmars-d-learn mailing list