January 14, 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);

}
January 14, 2020
On Tuesday, 14 January 2020 at 12:05:01 UTC, John Burton wrote:
> After years of C++ I have become paranoid about any casting of pointers being undefined behavior due to aliasing so want to see if :-

FWIW, this is safe and portable in C++20:

https://en.cppreference.com/w/cpp/numeric/bit_cast


> 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?

What is legal in C, should in theory be legal in D unless the documentation states otherwise as the goal for D is to make porting C code to D easy. Hence, type punning through union should be ok.

I am not sure what the various D compilers do with aliasing, but allowing type punning through pointers can inhibit some optimizations.