this post was submitted on 31 Aug 2024
11 points (100.0% liked)

General Programming Discussion

7728 readers
17 users here now

A general programming discussion community.

Rules:

  1. Be civil.
  2. Please start discussions that spark conversation

Other communities

Systems

Functional Programming

Also related

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] SubArcticTundra@lemmy.ml 3 points 1 month ago (2 children)

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].

[–] velox_vulnus@lemmy.ml 3 points 1 month ago (1 children)

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

[–] SubArcticTundra@lemmy.ml 4 points 1 month ago* (last edited 1 month ago)

Hmm I think I saw it in a C library

Edit: Might have been this one https://github.com/msteinert/bstring

Edit: actually seems it's this one. Look at what happens to ystr_header_t https://github.com/Amaury/Ylib/blob/master/src/ystr.c

[–] tunetardis@lemmy.ca 1 points 1 month 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.