LalSalaamComrade@lemmy.ml to General Programming Discussion@lemmy.mlEnglish · 18 days agoAre there better alternatives to null-terminated strings?message-squaremessage-square11fedilinkarrow-up11arrow-down10
arrow-up11arrow-down1message-squareAre there better alternatives to null-terminated strings?LalSalaamComrade@lemmy.ml to General Programming Discussion@lemmy.mlEnglish · 18 days agomessage-square11fedilink
minus-squareSubArcticTundra@lemmy.mllinkfedilinkarrow-up0·18 days agoAnother 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].
minus-squareLalSalaamComrade@lemmy.mlOPlinkfedilinkEnglisharrow-up0·18 days agoBut wouldn’t this be potentially unsafe? What programming language has this type of implementation, by the way?
minus-squareSubArcticTundra@lemmy.mllinkfedilinkarrow-up0·edit-218 days agoHmm 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
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].
But wouldn’t this be potentially unsafe? What programming language has this type of implementation, by the way?
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