4 ms·
hmm, is it normal to assume all strings in C are \0 terminated? What are the memory usage implications for that? I won't pretend to know C but suppose you have
by djb_hackernews 16y ago
hmm, is it normal to assume all strings in C are \0 terminated? What are the memory usage implications for that?
I won't pretend to know C but suppose you have a string that is 'ab{100}\0' and you wanted to remove all of the bs, you'd end up with 'a\0b{99}\0' in memory correct?
- RiderOfGiraffes 16y agoC works at a very low level. You have lumps of memory, and you can put stuff in it. By convention a "string" is actually a lump of memory with chars in it, and the end is indicated by a '\0'. The lump stays the same size and doesn't need tidying. This is the source of the notorious "fgets" bug/hack/exploit. This is not the time or place for a tutorial on C strings and memory management, but suffice to say that some people find it horrible, some people find it easy and obvious. It stems from C being a sort of macro-assembler with a simplistic type system glued on top of it. ADDED IN EDIT: I don't know why you got down-voted - it's a reasonable question from someone who hasn't done C. I've up-voted you to get you back to "1".
- tptacek 16y agofgets() bug/hack/exploit? You mean gets(), right?
- RiderOfGiraffes 16y agoEr, probably. Brain fried, too much multi-tasking. Sorry.
- js2 16y agoYes, C strings are \0 terminated and that is what you'd end up with. Memory management is between you, malloc() and free(). Assuming this were malloc()d memory, if you wanted to free up the unused bytes, you'd probably: char *condensed = strdup(z_terminated); if (condensed) free(z_terminated); else // out of memory
- deleted 16y ago[deleted]
- nuxi 16y agoThat's the very definition of a C string - if it isn't \0 terminated, it's not a string. See section 4.1.1. of http://flash-gordon.me.uk/ansi.c.txt http://flash-gordon.me.uk/ansi.c.txt.
- rlpb 16y agoIt's convention for C strings to be \0 terminated, and the standard library expects this just about everywhere (along with the kernel and just about everything else you might want to interface with). If you wanted to store a string as a (length, data) pair you could do that too, but you'd have to write most of your own string manipulation functions.
- philwelch 16y agoInterestingly, if you did store your string as (length, data) pairs, strlen would be an O(1) rather than an O(n), which would also impact strcat. Also, strtok could possibly become non-destructive. The length value of the pair could be an 8 bit character value at the beginning of the array (which is how Pascal did it). That limits you to strings of length 255. If you represented strings as a struct containing an integer length and a char * pointing to the head of the array, the size limit of a string would in fact be no less than the limit of the length of any addressable array anyway. (Assuming that the head of the array is at memory address 1, the farthest it could possibly stretch is to the highest addressable memory value for an n-bit pointer. So an n-bit integer for the length value would clearly suffice.)
- jodrellblank 16y agoInterestingly, "Excel uses Pascal strings internally which is why strings in many places in Excel are limited to 255 bytes, and it's also one reason Excel is blazingly fast." - http://www.joelonsoftware.com/articles/fog0000000319.html http://www.joelonsoftware.com/articles/fog0000000319.html