3 ms·
Of course, assuming that unwanted space is there. With addr and len, you could split "helloworld" into "hello" and "world" by simply pointing to the two separat
by matthavener 15y ago
Of course, assuming that unwanted space is there. With addr and len, you could split "helloworld" into "hello" and "world" by simply pointing to the two separate parts, each with len == 5. (This is actually really nice when parsing large strings into smaller strings because you can simply point all the smaller strings into a single, large string).
- colomon 15y agoThat assumes that the string characters are not adjacent to the string length field in memory... (Edited to add) More explicitly: Grandparent specified length-prefix strings, where the string's length is stored in memory immediately before the string's characters. And his claim is correct in that case. You seem to be assuming a string object which consists of a length and a pointer to the characters. What you suggest certainly would work there, but any straight-forward implementation of this is going to require another pointer's worth of memory and two memory allocations for each string, too.
- ZoFreX 15y agoThis is actually what Java does when you call substr, effectively (String objects contain a reference to a char array, a length, and an offset).
- daemin 15y agoThat's also because (to my knowledge) that strings in Java are immutable. Same as in Ruby and a bunch of other languages. So the optimisation that can be done is to put all of the string data into one big (immutable) array and have each string object just reference into it for its data.