Live data from Hacker News

C Craft

crypto.stanford.edu

11–20 of 60 posts

Re: C Craft

#12
post #11

The first section I looked at contains a buffer overflow: http://crypto.stanford.edu/~blynn/c/ch03.html#_when_are_size... Great C craft :-)

Why is this downvoted? It is a serious bug in the example code.

Re: C Craft

#13
C provides my daily bread and I absolutely love the language.

That said, sometimes(I mean often) it does hurt when I have to work at a higher abstraction level in some fancier language and I keep thinking on what is going behind the scenes.

Re: C Craft

#14
post #10

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 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.

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 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

#15
well, he mentions hating OO languages because it makes it hard for him to program in a style similar to C. That's a bit like saying you hate hate hammers because they don't work with screws very well.

Re: C Craft

#16

well, he mentions hating OO languages because it makes it hard for him to program in a style similar to C. That's a bit like saying you hate hate hammers because they don't work with screws very well.

No, his probelm with OO is that inheritance is problematic as a primary means of abstraction. It sounds nice on paper, but can lead to horribly unmaintainable code and wasting time arguing over specifics of tangled inheritance hierarchies. (This is a bigger issue in statically typed OO languages - being able to just send a message and get an exception if the recipient doesn't support that interface helps quite a bit.)

Also, he says that OO languages (really, only some) have weak support first-class functions and closures, since they place so much emphasis on objects.

His criticisms sound more accurate for Java and C++ than, say, Python or Smalltalk. (He mentions Eiffel specifically, but I have no experience with it.)

Re: C Craft

#17
post #6
post #3

Honstly, OOP isn't that bad. This would be a lot better of a read if he focused on how C is good, not how he hates OOP. OOP is a valid way to solve any problem, though certainly not the only valid way. Incidentally I wrote an article in defense of OO: http://www.zideck.com/blog/article.php?id=1

This would be a lot better of a read if he focused on how C is good Then it would be a Twitter post, not a blog post.

This isn’t a blog post. It’s the preface to a longer work – contents at the left.

Re: C Craft

#18
post #10

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 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.

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

#19

I think the author of this document would really appreciate a lot of things in Go. Given his wish for := as the assignment operator, CSP semantics and some other things...

The author works for Google, and his most recent blog post is actually about the Go language:

http://benlynn.blogspot.com/2009/11/it-go-time_6644.html

Re: C Craft

#20
post #10

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 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.

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.

Post reply on HN