![]() ![]() Note that neither is ptrdiff_t guaranteed to be that signed version, or to hold any possible value in the domain of size_t or (strictly speaking) any possible object size. In the standard, it is used for byte counts in I/O, like the return value of read() (traditionally int), for the return value of strfmon() and strfmon_l() (OK I guess, though the C standard stuck with int for *printf()), and for the argument to swab() (wat). As best as I can tell, the only things POSIX says about ssize_t is that it is an integer type that can hold integer values in, where SSIZE_MAX ≥ _POSIX_SSIZE_MAX = 32767, not that it should have any particular relation to size_t. I say this because substring operations return references to pieces of the original string, thus you can't stuff a length in front of the actual characters of the substring, as this would over-write characters in the original string. Conversely, the Extended Pascal standard pretty much requires strings to instead be implemented as "fat pointers," consisting of a length and a pointer to the actual characters. The topic at hand was "How does Pascal store strings?" I stand by the pedantic statement that ISO Standard Pascal does not require string lengths to be stored with the string (nor anywhere else) there's no way for the programmer to obtain the length of a string at compile-time, never mind run-time, without resorting to some sort of compiler-specific language extension (most (all?) of which did indeed put the string length in front). ![]() If you wanted to write run-everywhere software, Modula-2 was never a tenable choice.īut we digress. No doubt it was a fine language, but it never caught on to anywhere near the extent Pascal had, before C proceeded to take over the world. This phrasing might give the wrong impression Modula-2's practical availability was extremely limited. No, I was simply responding to your statement that "Modula-2 was already available in 1978". I have no evidence for nor against advantage #3 though. It is a conscious decision to whether you should iterate over bytes or Unicode scalar values or code points or grapheme clusters, and for this reason it is better to make the decision explicit even though it's C `char` in the surface level. And I have more counterpoints to those self-claimed advantages as well:Ĭounterpoint #1: You can't pass SDS strings to functions that accept `char **` (which is a common way to return a string of unknown length, and often can act as an in-out parameter as well).Ĭounterpoint #2: You rarely access individual "characters" (whatever this means). ![]() ![]() No other type in C behaves like this, resulting in more confusion and thus more errors. This kind of "masquerading" pointer is conceptually a linear type, as you can't safely change its length in place and any potential change has to return the modified pointer somehow. My biggest complaint is that it's a data structure disguised as a single naive pointer, which is actually harder to use correctly. Since modern programs performances are often dominated by cache misses, SDS may perform better in many workloads. Over the time the buffer is reallocated, and it is likely that it ends in a totally different part of memory compared to the structure itself. Usually when you access a string created by a string library using a structure, you have two different allocations for the structure representing the string, and the actual buffer holding the string. The trick is the size is hidden before the adress of the buffer.("Learn this one simple trick that will change your life for ever").Īdvantage #1: you can pass SDS strings to functions designed for C functions without accessing a struct member or calling a functionĪdvantage #2: accessing individual chars is straightforward.Īdvantage #3: single allocation has better cache locality. Thanks to the someone who recommended it on HN some years ago. I always use antirez's (Redis creator) `sds` and advertise it whenvever I get the chance. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |