Why am I getting segfaults when doing `foreach` with arrays of references?

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sat Mar 9 06:37:02 UTC 2024


Something that I have noticed that you are still doing that was pointed 
out previously is having a pointer to a class reference.

Stuff like ``Tile* currentTile;`` when it should be ``Tile currentTile;``

A class reference is inherently a pointer.

So when you checked for nullability in the foreach loop of mission:

```d
if (startTile.occupant !is null && (*startTile.occupant) !is null) {
```

is what it should've looked like.

```d
import std.stdio : writeln;

void main() {
     Object o = new Object;
     writeln(cast(void*)o); // 7F2780EFD000
}
```


More information about the Digitalmars-d-learn mailing list