• SubArcticTundra
    link
    fedilink
    arrow-up
    3
    ·
    14 days ago

    Another alternative I’ve seen is strings that are not null terminated but where the allocated memory actually begins at ptr[-1] and contains the length of the string. The benefit is that you still get a char array starting at ptr[0].

    • LalSalaamComradeOP
      link
      fedilink
      English
      arrow-up
      3
      ·
      14 days ago

      But wouldn’t this be potentially unsafe? What programming language has this type of implementation, by the way?

    • tunetardis@lemmy.ca
      link
      fedilink
      English
      arrow-up
      1
      ·
      14 days ago

      This reminds me of when I had to roll my own dynamic memory allocator for an obscure platform. (Something I never want to do again!) I stuck metadata in the negative space just before the returned pointer like you say. In my case, it was complicated by the fact that you had to worry about the memory alignment of the returned pointer to make sure it works with SIMD and all that. Ugh. But I guess with strings (or at least 8-bit-encoded strings), alignment should not be an issue.