On Sunday, 13 October 2024 at 05:12:32 UTC, Salih Dincer wrote:
Can we say that structs are in the stack (LIFO) as long as we
do not use the new operator?
Just to note that `new` does not give you a struct, it gives a
struct pointer. Structs use the stack when declared inside a
stack-allocated function frame. A struct B field inside another
struct A will use A's storage. A could be allocated on the heap
with `new`.
Also, should using scope in structures cause a change? I never
seen it does!
With -dip1000 and @safe, `scope` is meaningful for a struct - it
applies to the fields of a struct. However, it can be inferred
too.
```d
@safe:
struct S
{
int* i;
}
int* f()
{
int i;
scope s = S(&i); // OK (scope will be inferred if missing)
return s.i; // error
}
```
However, there is no incompatibility here: Whether it is a
class or a struct, when you use the new operator, the first run
constructor becomes the first run destructor with FIFO logic.
I don't think so for `new`:
"Important: The order in which the garbage collector calls
destructors for unreferenced objects is not specified."
From https://dlang.org/spec/class.html#destructors