Permanently changing time string with smart strings?

In C, instead of keeping track of just the length of a string, what if we track its direction? For example, if str = "hello"

it was laid out in memory (as indicated by malloc), we would say that it has a positive direction (+1) and therefore passes +1 to the function arguments. Instead of taking O (n) to reverse that line, now we can just say rev = str[size - 1]

with direction -1 and so it is handled differently. I understand that null termination can cause some problems, but assuming we have a string length and a starting character in memory, we don't need to worry about null termination (correct me if I'm wrong).

Is this a viable option if line switching is critical in a program? Can anyone give me a reason why I shouldn't do this? Has anyone heard or seen this?

Sorry if my formatting is off, this is one of my first posts. Thanks in advance!

+3


source to share


1 answer


I am sure that in order to change the line, you will have to copy the line somehow. Outside of a kind of MMU trick, where reads from system memory are changed at the addressing level, this requires the need to traverse the line at least once. Even if you copy 4/8/16 + bytes at a time, it is still a linear operation.



Now, if you can just read the string in place, in reverse order (using a special iterator or whatever), then perhaps you could consider this O (1) operation, provided that this special iterator is also O ( 1).

+2


source







All Articles