Live data from Hacker News

C for high level programmers (slides)

charliethe.ninja

51–60 of 129 posts

Re: C for high level programmers (slides)

#51

Earlier quoted context omitted.

Unfortunately, UB extends far beyond that. To the point of being (very) logically inconsistent. People not familiar with C would naively expect that, for instance, `x[10] == x[10]` is always true even if 10 is out-of-bounds for x (or rather: it may crash, or it may be true.) . But compilers can - and will - assume that this situation is false if it makes the code faster. This sort of thing is my major pain point with…

Not only can the compiler make it false. It can assume that the code would never make the comparison in the first place, and optimize away any cases where the undefined behavior is guaranteed to be triggered. So in a sense undefined behavior can travel back in time :O

Yep.

It's impossible to have actual modular code in C. A real shock for people coming from higher level languages.

Re: C for high level programmers (slides)

#52
post #21

The thing about pointers that I don't get is why do you need the memory address of the variable? Is that the only way to get the value when you want it? Like, every variable has to have a pointer in order to make use of the variable?

If you want arrays of variables, you need pointers. It's not enough to know the value of an int (the first element of the array), but you need the pointer to an int (pointer to the first element of the array). Now you can increment the pointer to go to the next element. You couldn't do this with a simple value. Also, suppose you want to pass a variable of a large data type, like an image, to a function. Instead of co…

Of course the problem is, if you're passing a pointer to a data structure to a function, the function doesn't know the size of the data structure unless you pass that as another argument.

Re: C for high level programmers (slides)

#54
post #42

As someone who just reread "C Programming Language", in chapter 1 they teach you how to count occurrences of characters so I'm not sure I understand the sarcastic tone of "you're not going to count foo in a file". C can be used for many things, I don't think the author of the slides dod a great job of describing "when" C is the right tool for the job.

I think he is just saying that C isn't worth learning if you just need to do really simply things that a ton of modern languages can do in one of two lines.

Or, you know, old-school 'programming' by combining Unix utilities from the 1980s.

    $ wc foo file.txt

Re: C for high level programmers (slides)

#55
post #38
post #7

Earlier quoted context omitted.

Hey author of the slides here, glad you liked it :D I used http://remarkjs.com/ to make the slides. All you have to do is include the script, add a textfield with your content in markdown and it automatically converts to a slide show.

Can you tell us what font you used?

It's all there in the CSS. Looks like "Amaranth" and "Droid Serif".

Re: C for high level programmers (slides)

#56
post #31

The thing about pointers that I don't get is why do you need the memory address of the variable? Is that the only way to get the value when you want it? Like, every variable has to have a pointer in order to make use of the variable?

You don't need the address of a variable: you can use the plain variable just fine, and most C code does a lot of that. What a pointer does is add a level of indirection: so instead of having a value "an integer" you can have a value which is "the location of an integer". A variable holding such a value can be assigned the location of any integer variable, and importantly can also be reassigned the location of a diff…

And why is this useful? Well, for high-level folks, pointers are used for roughly the same thing as reference variables in other languages.

For low-level folks, sometimes you need to be able to read from / write to a specific address in memory. So if you have, for instance, a system clock device that always give you the current time if you read address 0x1234, you might do something like this:

uint64 system_time;

uint64 system_time_device = 0x1234; // A pointer to the system time device...

system_time = system_time_device; // read the contents of the memory at address 0x1234 to get the time

Re: C for high level programmers (slides)

#58
post #7

I just about died laughing at slide 22. Anybody know how these slides were made?

Hey author of the slides here, glad you liked it :D I used http://remarkjs.com/ to make the slides. All you have to do is include the script, add a textfield with your content in markdown and it automatically converts to a slide show.

Intersting how simple it is. `class` support is nice to grab the attention (e.g: the black slides)

Re: C for high level programmers (slides)

#59
As someone who occasionally dabbles in C code, I am interested in knowing what is the modern take on `goto`s.

I know they are "harmful" but I often come across code riddled with goto statements [1] and I personally feel that as long as it makes the code readable without significantly obfuscating the logic, goto is a perfectly fine way of doing things (although popular opinion and consideration for best practices have more or less forced me to remove goto from my list of C tools). Also, the fact that it maps almost directly to asm makes it easier to reason about the generated machine code (although if that's a significant reason for using goto is questionable).

[1] https://github.com/zedshaw/mongrel2/blob/master/src/http11/h...;

Zed Shaw's Mongrel2 server code linked to in another thread.

Re: C for high level programmers (slides)

#60

As someone who occasionally dabbles in C code, I am interested in knowing what is the modern take on `goto`s. I know they are "harmful" but I often come across code riddled with goto statements [1] and I personally feel that as long as it makes the code readable without significantly obfuscating the logic, goto is a perfectly fine way of doing things (although popular opinion and consideration for best practices have…

Goto is fine. The 'harmful' style was using them in favor of ifs and loops.

The things I've seen people do to avoid a goto are pretty awful though. If you ever use a 'do {} while (0)' just to break out of it, you should feel bad. Goto is much clearer and cleaner than nonsense like that.

Post reply on HN