News for hackers. Stuff that matters.
Re:Interviewing programmers: coding test example explained by djb_hackernews on Thursday June 03, 2010 @09:44AM (#1401252)
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?

Re:Interviewing programmers: coding test example explained by RiderOfGiraffes on Thursday June 03, 2010 @09:47AM (#1401263)
C 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".

Re:Interviewing programmers: coding test example explained by tptacek on Thursday June 03, 2010 @01:41PM (#1401936)
fgets() bug/hack/exploit? You mean gets(), right?
Re:Interviewing programmers: coding test example explained by RiderOfGiraffes on Thursday June 03, 2010 @01:45PM (#1401948)
Er, probably. Brain fried, too much multi-tasking. Sorry.
Re:Interviewing programmers: coding test example explained by js2 on Thursday June 03, 2010 @09:53AM (#1401280)
Yes, 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
Re:Interviewing programmers: coding test example explained by cperciva on Thursday June 03, 2010 @10:26AM (#1401388)
Using realloc would be better, since it allows the memory allocator the option of not copying the string.
Re:Interviewing programmers: coding test example explained by js2 on Thursday June 03, 2010 @10:59AM (#1401479)
Indeed. I wasn't sure you could realloc to a smaller block. Good to know.
Re:Interviewing programmers: coding test example explained by rlpb on Thursday June 03, 2010 @12:49PM (#1401800)
It'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.
Re:Interviewing programmers: coding test example explained by philwelch on Thursday June 03, 2010 @06:55PM (#1402889)
Interestingly, 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.)

Re:Interviewing programmers: coding test example explained by jodrellblank on Friday June 04, 2010 @11:34PM (#1406293)
Interestingly, "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
Re:Interviewing programmers: coding test example explained by nuxi on Thursday June 03, 2010 @12:40PM (#1401773)
That'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.