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

  • Scope & Stru... Salih Dincer via Digitalmars-d-learn
    • Re: Scope &... Salih Dincer via Digitalmars-d-learn
      • Re: Sco... Nick Treleaven via Digitalmars-d-learn
        • Re:... Salih Dincer via Digitalmars-d-learn
          • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: Scope &... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: Sco... Salih Dincer via Digitalmars-d-learn
        • Re:... Nick Treleaven via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Salih Dincer via Digitalmars-d-learn

Reply via email to