Hacker News new | past | comments | ask | show | jobs | submit login

Most if not all string algorithms will eventually do this anyway:

  size_t length = strlen("some string");
It's so common. Might as well memoize it so it's always available with no need to constantly loop through strings which is an O(N) algorithm. So many string algorithms call strlen, often multiple times on the same string. I remember GTA V took 6 minutes to parse a goddamn JSON file because of stuff like this and part of the fix was to store the string lengths.

https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times...

So is a length variable really such a big deal? It even fits in a register.

I understand and agree with your one memory span point. Ideally they should be located as close as possible in memory.

  struct bytes { size_t size; unsigned char data[]; };

  const char *literal = "some text";
  size_t length = strlen(literal);

  struct bytes *text = malloc(sizeof(*text) + length);
  text->size = length;
  memcpy(text->data, literal, length);



I'm not anti-length-variable! I'm saying, which of

  struct bytes { size_t size; unsigned char *data; };
  struct bytes { size_t size; unsigned char data[]; };
you want depends heavily on what you're doing with the string(s) - plus other common variations like len+cap instead of just size, SSO, etc.

So if you can't standardize the data structure, what's the common interface? A function that takes a pointer and a length - which is what we already have. So everyone in this thread appealing to the C standardization process or stdlib to do something wants instead - what, exactly?




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: