Live data from Hacker News

What can you do in 2k LOC of C?

h4ck3r.net

71–80 of 110 posts

Re: What can you do in 2k LOC of C?

#71
post #18

How much memory does the code the blog poster described leak? How does it respond to edge cases and invalid input? Finally, is it portable beyond one specific OS? Beyond POSIX or Windows-based systems? Those questions are especially pertinent in C.

Right! Coming up with standalone C means coming up with your own versions of hard-to-write simple "system calls" like memove.

I debugged a memcopy (taken from Linux! 10 years ago) on a RISC processor - it had 12 bugs in a dozen lines of code. Not designed to run on RISC but shows how hard it can be to get this stuff right.

Re: What can you do in 2k LOC of C?

#72
post #20

Getting a standards-compliant XML parser into 2K lines is going to be a challenge, if you're not going to cheat on what a "line" is. You must be able to deal with both UTF-8 and UTF-16 [1] (and remember UTF-16 can be in either endian order), you have several tables of things like what chars are valid where, you've got data structures to declare, and there's a lot of edge cases that may not leap to mind but if you don…

A JSON parser (of sorts) in roughly 150 lines of C code. I'm not the author.

https://github.com/quartzjer/js0n

Re: What can you do in 2k LOC of C?

#73

Earlier quoted context omitted.

> This is also why we have many more softwares available now. Assuming English isn't your first language, that would be phrased "much more software"; software is always singular.

http://www.ar.media.kyoto-u.ac.jp/members/david/ "David Cournapeau a French PhD student in signal processing at Kyoto University" France and Japan...he hasn't exactly been living in places where English is prominently spoken with grammatical accuracy...cut him some slack. In fact, he made the exact same error of pluralizing "software" as "softwares" on his website.

> cut him some slack

You act as if I'm giving him a hard time, I'm not. I've generally found multi-lingual people want to be corrected when they do it wrong because they find it helpful; I was doing him a favor.

Re: What can you do in 2k LOC of C?

#74
post #62

Earlier quoted context omitted.

Or perhaps it's people who mistake obfuscation for elegance and brevity.

I'd love to hear why that code doesn't embody brevity? As for obfuscation, it's rather straightforward -- it's just concise . A lack of instant understanding doesn't indicate that a piece of code is without merit, or not honest and straightforward.

To be clear. If someone submitted this code to you for code review you'd say, "nice and concise -- approved"? Really?

I get this person may have been working with constraints we don't know about, but its not straightforward. Give this to 10 working C devs and ask them to tell you what it does without running it.

The standard cues of straightforward code aren't there -- well named variables, well named functions, comments, indentation, etc...

Re: What can you do in 2k LOC of C?

#75
post #46

Fabrice Bellard's IOCCC entry of 2002 was a C-subset compiler in 617 wc-lines (ELF version). http://bellard.org/otcc/

It's an amazing piece of work: Those 617 lines are in its own C subset, so it can compile itself into a native code ELF version.

This evolved into bellard's "tcc" compiler, which is the fastest (compile-time wise) available on linux, the only one I'm aware of that can be used as a library to run code in memory (without having to generate an .so and load that). And it's output performance, while lacking compared to -O3 gcc or clang - is not too shabby!

Re: What can you do in 2k LOC of C?

#76
post #49
post #20

Getting a standards-compliant XML parser into 2K lines is going to be a challenge, if you're not going to cheat on what a "line" is. You must be able to deal with both UTF-8 and UTF-16 [1] (and remember UTF-16 can be in either endian order), you have several tables of things like what chars are valid where, you've got data structures to declare, and there's a lot of edge cases that may not leap to mind but if you don…

> Getting a standards-compliant XML parser into 2K lines is going to be a challenge This is a pretty good argument against XML.

Yes, that point may have had some influence on the way I phrased my post.

Re: What can you do in 2k LOC of C?

#77

Earlier quoted context omitted.

Excuse me, but in my programming thought flow, there are checksums and limits, not c's and l's, and my code reflects that. Do I not qualify as a coder?

Quick, what does the i stand for in "for (i=0; i It was a quick prototype written by one APLer, for another APLer, using mutually understood conventions. For example, its dyadic verbs ("V2" functions) use the variable names 'a' and 'w'. For a while, I wondered why (maybe they stood for "Arthur" and "Whitney"?), but after reading Iverson's _A Programming Language_, I realized APL usually uses lowercase alpha (α) and o…

Wow, thanks for that clue. Will have to take a closer look at that source now. I don't feel too bad for not really getting it, given the creator of J saying this about it:

My immediate reaction on seeing the page was recoil and puzzlement: it looked nothing like any C code I had ever seen. (“Is it even C?”) However, Ken counselled that I should reserve judgment. As recounted in An Implementation of J, it then happened that:

I studied this interpreter for about a week for its organization and programming style; and on Sunday, August 27, 1989, at about four o’clock in the afternoon, wrote the first line of code that became the implementation described in this document.

The name “J” was chosen a few minutes later, when it became necessary to save the interpreter source file for the first time.

Re: What can you do in 2k LOC of C?

#78

Earlier quoted context omitted.

I'd love to hear why that code doesn't embody brevity? As for obfuscation, it's rather straightforward -- it's just concise . A lack of instant understanding doesn't indicate that a piece of code is without merit, or not honest and straightforward.

To be clear. If someone submitted this code to you for code review you'd say, "nice and concise -- approved"? Really? I get this person may have been working with constraints we don't know about, but its not straightforward. Give this to 10 working C devs and ask them to tell you what it does without running it. The standard cues of straightforward code aren't there -- well named variables, well named functions, comm…

I upvoted you, but as mentioned earlier in this thread by silentbicycle, this was written by one APLer to be read by another. I suppose even knowing APL makes it still difficult to follow, but only as difficult as APL itself is to follow right? :)

Re: What can you do in 2k LOC of C?

#79

Shocked that silentbicycle hasn't mentioned it already, but Arthur Whitney whipped up the first prototype/inspiration for the J language in a short bit of macro heavy C over the course of an afternoon. 42 lines? http://pastebin.com/s2usuqDq If this interests you at all, absolutely worth reading Roger Hui's retrospective on the subject (more about J + Ken Iverson, but definitely fascinating) http://keiapl.org/rhui/

On a related note, reading J's getting started guide has been pretty interesting so far: http://www.jsoftware.com/help/primer/contents.htm

Re: What can you do in 2k LOC of C?

#80
post #77

Earlier quoted context omitted.

Quick, what does the i stand for in "for (i=0; i It was a quick prototype written by one APLer, for another APLer, using mutually understood conventions. For example, its dyadic verbs ("V2" functions) use the variable names 'a' and 'w'. For a while, I wondered why (maybe they stood for "Arthur" and "Whitney"?), but after reading Iverson's _A Programming Language_, I realized APL usually uses lowercase alpha (α) and o…

Wow, thanks for that clue. Will have to take a closer look at that source now. I don't feel too bad for not really getting it, given the creator of J saying this about it: My immediate reaction on seeing the page was recoil and puzzlement: it looked nothing like any C code I had ever seen. (“Is it even C?”) However, Ken counselled that I should reserve judgment. As recounted in An Implementation of J, it then happene…

If it intrigues you, check out Henry Rich's _J for C Programmers_. There's a free PDF on Lulu, and it's included with J (http://www.jsoftware.com/).

Like Emacs, J's internal documentation is excellent, but you have to learn its idiosyncratic terminology before you can actually find things.

Post reply on HN