Hacker News new | ask | show | jobs
by JohnFen 1115 days ago
Sure. For instance, there are times when you need to pack strings tightly together. Adding an extra byte or two before the start of the string would get in the way. You could work around it in many cases, but it makes the code uglier and harder to understand/maintain.

One of the things that makes C particularly suitable for certain sorts of tasks is that it's mostly WYSIWYG when it comes to the relationship between data structures and the actual memory layout. Having "hidden" things like a length value before the string steps on that.

2 comments

I agree on the first paragraph, but the second one applies poorly to strings:

  char *s = "hello";
"hello" has length 6 because there's a hidden \0 even if I never wrote it in the code.
if you wanted to pack strings together tightly, couldn't your string library have a separate "array" concept where all the sizes are stored separately?