News for hackers. Stuff that matters.
Re:C Craft by doty on Friday December 18, 2009 @10:53AM (#1003282)
I think it's telling that the very first example to use dynamic allocation has a memory corruption bug.

  struct {
    int n;
    char c[0];
  } *foo = malloc(16); /* sizeof(*foo) probably 4, so 12 bytes follow */

  foo->n = 16;
  for(int i = 0; i < 16; i++) { /* puts 16 bytes after foo->n. oops. */
    foo->c[i] = 'a' + i;
  }
Not that I don't love C for terseness and power or appreciate the systems one can build in it, but seriously.
Re:C Craft by jgrahamc on Friday December 18, 2009 @10:58AM (#1003298)
I noticed that too, and what's weird is that's not at all how you should do it. Since the char c[0] has zero size you can do sizeof on the struct so that you can do the malloc correctly.

  struct thing {
    int n;
    char c[0];
  };

  int i_need = 16;
  struct thing * foo = malloc(sizeof(struct thing)+i_need);
  foo->n = i_need;

  for (int i = 0; i < foo->n; i++ ) {
    foo->c[i] = 'a' + i;
  }
And if your compiler won't let you do char c[0] you can do char c[1] and either blow a byte, or do the calculation correctly. Note that you have to be careful with this sort of allocation if you are on a machine that has certain alignment requirements (e.g. Sun).
Re:C Craft by cpr on Friday December 18, 2009 @01:43PM (#1003660)
Well, being pedantic, that should be

  struct thing *foo = malloc(sizeof(struct thing) + sizeof(char) * i_need);
since sizeof(char) isn't guaranteed to be 1 byte on all systems.
Re:C Craft by pavlov on Friday December 18, 2009 @02:06PM (#1003714)
Actually, the C standard guarantees that char is equal to one byte.
Re:C Craft by neild on Friday December 18, 2009 @02:18PM (#1003741)
More to the point, it defines a byte in terms of a char. sizeof(char) will always be 1, but UCHAR_MAX need not be 255.
Re:C Craft by Locke1689 on Friday December 18, 2009 @12:15PM (#1003466)
His warning against using #ifdef is also misplaced. I have written assembler (GAS) using the GAS x86 asm preprocessor to write portable x86/x86-64 assembly code in one file. Far simpler and shorter than writing the same code in two files.

Basically, whenever he says "never" do something he means he doesn't like to do it. For example, his answer to not having a multi-level function break is a nested helper function. God no. That's one of the few times when it's correct to use goto.

Edit: Ahh, he also advocated always using mmap instead of the standard file IO functions. Wow that's bad.

Re:C Craft by mbreese on Friday December 18, 2009 @12:54PM (#1003551)
I'm curious: are the standard IO functions that much more efficient than a mmap'd file? What makes you choose one over the other?

I usually just use standard IO, unless I have a large file that might be randomly accessed by multiple processes. In that case I'll use a mmap. But, I don't code in C very much anymore, so I was wondering how much of a difference there really is.

Re:C Craft by sorbits on Sunday December 20, 2009 @12:07PM (#1006303)
I did benchmarks on OS X (10.5) between asynchronous IO, mmap, stdio, and normal unix open/read (disabling caching) — it all came down to about the same after I had fiddled with page size, prefetch size, etc. to optimize each.

This of course depends on your OS (and version). My advice would be to pick API based on “semantics” (or need for portability). If you need to read through a file, use the API intended for that because mmap will not be optimized for that access pattern, but stdio will.

Re:C Craft by Locke1689 on Friday December 18, 2009 @08:39PM (#1004377)
Mostly because mmap is not portable. If you're writing a Linux specific version of your code then mmap is fine. If you foresee running your code on other platforms then you shouldn't use it.

Basically, there's a reason why it's called "standard" IO. Generally you shouldn't use system calls unless you are absolutely, 100% sure that your code will only ever need to work on one system.

Re:C Craft by holygoat on Friday December 18, 2009 @08:53PM (#1004391)
I've worked on a project that successfully uses memory mapping on all the major OSes.

mmap is (allegedly) portable across Unixes:

http://opengroup.org/onlinepubs/007908775/xsh/mmap.html

and you can do the same thing (with a few more hoops) on Windows.

Re:C Craft by kelnos on Saturday December 19, 2009 @04:32AM (#1004630)
Many people may not care, but mmap() often doesn't exist on hardware without a MMU.
Re:C Craft by Locke1689 on Friday December 18, 2009 @10:21PM (#1004446)
There's no guarantee that it will be portable and act the same (even across the POSIX systems). I'm not saying you should never use it, just that it shouldn't your automatic answer to everything.
Re:C Craft by jlangenauer on Friday December 18, 2009 @07:24PM (#1004307)
I'm curious about this too - why is mmap so bad?
Re:C Craft by holygoat on Friday December 18, 2009 @08:50PM (#1004389)
It's not always bad -- very often it's the right thing to do. Just not "always".
Re:C Craft by jimbokun on Friday December 18, 2009 @11:56AM (#1003424)
Thank goodness. I haven't programmed C in a long time, and thought I was missing something when I saw that code.
Re:C Craft by zephjc on Friday December 18, 2009 @12:39PM (#1003522)
Give a man enough rope to hang himself, and he'll shoot himself in the foot.