• SpaceNoodle@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    3 months ago

    I don’t think I’ve ever explicitly gone four deep. Two is common enough, and three happens on some rare occasions, but four seems like sheer madness.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      0
      ·
      3 months ago

      Dumb question, but when would you need two deep? Is it when you store a pointer as a field in a struct?

      If so, isn’t that a massive footgun, because the pointer might go invalid at any point? 🫠

      • SpaceNoodle@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        3 months ago

        Pointers to arrays or arrays of pointers are common examples.

        Your pointers won’t just magically become invalid. You gotta fuck 'em up first.

        • Ephera@lemmy.ml
          link
          fedilink
          arrow-up
          0
          ·
          3 months ago

          Ah, you mean “pointers to arrays”, because arrays are themselves just pointers in C/C++. That one still feels like it shouldn’t be needed in practice, because you already got a pointer, why can’t you use that directly? But yeah, I have no practical experience with C/C++.

          And you do gotta fuck 'em up, as in free what they’re pointing to before you free the struct/array containing the pointers.
          But when you do stick them into a struct/array, that often means you want to move them out of the scope with the malloc and potentially store them, too.
          At that point, surely, it becomes rather difficult for your whole team to know or track down when it’s legal to free that.

          I only know from Rust that if you want to store a pointer/reference in a struct, it makes you specify the lifetime of that struct, which has to be greater or equal to the lifetime of the thing the pointer is pointing to. Hairy stuff. We’ve basically told the Rust newbies on our team to just not store pointers in structs and that’s working rather alright.

          • SpaceNoodle@lemmy.world
            link
            fedilink
            arrow-up
            0
            ·
            3 months ago

            Arrays may be implemented as pointers on C, but the distinction is on how they are used, which is why I used the verbiage I did.

            What if you need to modify a reference to a pointer, e.g. change the value of a value referencing a certain place in an array? strtol(), for example, uses a pointer to a pointer to a char to indicate the end of the parsed portion of the input string.

            Major codebases performing high-level operations on data that’s shared in barely trackable scopes certainly aren’t best implemented in C. It’s still the language of choice for low-level code, especially on embedded systems, where allocations are not taken lightly.