Lurker News

Comments (218) 56 threads

Show all comments

Showing thread 1 of 56

faxmeyourcode2 replies#
This is some of the cleanest, modern looking, beautiful C code I've seen in a while. I know it's not the kernel, and there's probably good reasons for lots of #ifdef conditionals, random underscored types, etc in bigger projects, but this is actually a great learning piece to teach folks the beauty of C.

I've also never seen tests written this way in C. Great work.
C was the first programming language I learned when I was still in middle/high school, raising the family PC out of the grave by installing free software - which I learned was mostly built in C. I never had many options for coursework in compsci until I was in college, where we did data structures and algorithms in C++, so I had a leg up as I'd already understood pointers. :-)
Happy to see C appreciated for what it is, a very clean and nice/simple language if you stay away from some of the nuts and bolts. Of course, the accessibility of the underlying nuts and bolts is one of the reasons for using C, so there's a balance.

ashtonjamesd1 reply#
Wow! That really means a lot because I always make a lot of effort to make sure my code is just that :)

Appreciate you saying that!

jacquesm#
You've done a couple of things right: very few dependencies, simple, easy to understand code. C gets hairy when you try to be clever.

I'm busy writing some of the most optimized-but-still-portable code that I've ever written and it is very interesting to see how even a slight difference in how you express something can cause a massive difference in execution speed (especially, obviously, in inner loops). Your code is clearly written from what your comfort zone with C is and I'm really impressed by the restraint on display. At the same time, some of the code feels a bit repetitive and would benefit from more universal mechanisms. But that would require more effort and I'm not even sure if that is productive. One part where I see this is in the argument parsing code as well as in the way you handle strings, it is all coded very explicitly, which substantially increases the chance of making a mistake.
Another limitation is that using AI to help you write the code means you don't actually understand what it does, and this in turn may expose you to side effects that you are not able to eliminate because you did not consider them while writing, it is as if someone else gave you that code and asked you to trust them they did not make any mistakes.

citizenpaul15 replies#
> I'd already understood pointers.

Ok I hear this all the time. Are pointers really that hard for so many people to understand? I'm not trying to brag it took me I think like 15 minutes to grok them from learning about them the first time. I'm sure it took me longer to be proficient but I don't get this legendary difficulty aura that seems to surround their existance.
Also yes nice project.
Job app complete projected archived and abandoned in 3...2..1... :). I hope not.

copperx1 reply#
The concept is straightforward. The syntax isn't. That's why cdecl.org exists.
citizenpaul#
C gibberish to English gave me a chuckle thanks.
assimpleaspossi1 reply#
Same here about pointers. Perhaps it's cause I started life as an electronic engineer and understood memory addressing from the chip level but I, too, don't understand the struggle others seem to have.
citizenpaul#
I started in networking so there were a lot of memory/bit-logic/binary concepts piled on early maybe that is the case.
noufalibrahim1 reply#
It's a rabbithole. Pointer to array of structures that have pointer fields. Array of pointers to structures etc. You pass them around and trip over the passing semantics, uninitialised pointers etc etc.
citizenpaul7 replies#
Hmm. Perhaps I've just never encountered a hairy enough situation with them? That's what the eternal thought tracker notepad on my desk is for though. Maybe people are trying to do it all in their head? Pen and paper are too old school for the cool new 1000x devs?

I still feel like this argument could be transferred to nearly any concept in CS though. Abstract enough anywhere and you will always start exceeding the brains working memory.

Continue this thread — 7 replies

wkjagt#
I understood them on a superficial level when first learning about them, but it only really clicked after having done assembly.
leptons#
I learned assembly language long before I learned C, so pointers took me about 2 seconds to understand. I suppose it may depend on previous experience.
DanielHB#
> Are pointers really that hard for so many people to understand?

Yes, anyone who has taken algorithms and data structures class in C knows that some people just don't get it.
Also the way people teach it tends to be bad, before teaching pointers you need to teach Stack and Heap at a conceptual level.

lelanthran2 replies#
> Are pointers really that hard for so many people to understand?

Apparently they are; I believe it's the indirection that gets people.
Most learners aren't really taught basics properly - they learn that a variable "contains" a value, when instead they should learn that all values have a type, and some variables hold values.
> I'm not trying to brag it took me I think like 15 minutes to grok them from learning about them the first time.
I can't remember not knowing pointers, so I can't really tell you how long it took for it to click, but I do know that I had done a non-trivial amount of assembly before I used C, so maybe that helped/.

ranger_danger6 replies#
I think it would help a lot if pointers were taught to people from the perspective of how they actually occupy memory, and what the value it stores in memory represents, and then how that value is an address that is followed when a pointer is "dereferenced", etc.

It seems a lot of people assume that pointers don't actually consume any memory and then get confused trying to understand it that way.

Continue this thread — 6 replies

supportengineer2 replies#
Pointers make perfect sense.

Now dependency injection, that's some magical bullshit right there.

Continue this thread — 2 replies

dingdingdang1 reply#
The legendary status is also enhanced by the absolute nightmare that pointers enable if used with indiscretion or high level proficiency - a triple pointer is a good example for me but there's many many more, and arguably worse, examples out there.
koolba#
My favorite is pointers to functions passed as args for things like supplying a comparator. With more opaque void* args for customizing things even further.
faxmeyourcode#
Pointers in and of themselves are not difficult to learn on their own, but when you're learning them alongside your first programming language, it's just adds to the difficulty I think.

I think a lot of noobs learning C struggle with pointers especially because there are no good error messages besides "segmentation fault" :D

codegeek2 replies#
"Are pointers really that hard for so many people to understand?"

The * vs & always gets me and not to mention if I ever have to deal with Pointer Math.

TZubiri#
*pointer = what pointer points to &thing = address of thing
bionsystem4 replies#
Think of it as types. All of the following are the same thing (declare p as an int* type). It's important for the end :

  int * p;
  int *p;
  int* p;
Now remember that the type is a memory address. I'm sure it is semantically wrong for whatever reason somebody will explain but it helps to think about it. So you can do :

  int my_number = 6;
  int* p = &my_number;
Both sides of the "=" are the same type (int* is an address, and &my_number is also an address, the one of my_number).

Now p is a pointer (or an int* or an address), and *p is... an int ! So this is totally valid :

  printf("%d\n", *p)
and for anything else than int you need to malloc that so you will see a lot of :

  my_struct* s = malloc(sizeof(my_struct);
which makes sense because malloc returns an address (the beginning address of the content of s ; yet again somebody will tell me I'm wrong to call it "the content of s" so sorry for that).

  my_struct* // is the type of s, it is an address
  my_struct // is the type of *s (some custom type of size sizeof(my_struct))

Continue this thread — 4 replies

jug2 replies#
The worst thing with C pointers was for me that the asterisk is inexplicably used both to declare a pointer and a COMPLETELY different operation of dereferencing a pointer.

I still don't understand this decision. I think it should've been like int^ p = &i; ... or ... int i = *p;
Everything clicked ironically when I went even deeper and studied assembly language. Then following pointers to data vs just reading pointers becomes very clear and explicit.

sgsjchs1 reply#
> I still don't understand this decision.

Variable declaration `T v;` means "declare `v` such that expression `v` has type `T`". Variable declaration `T *p` means declare `p` such that the expression `*p` has type `T`". etc.

Continue this thread — 1 reply

tealpod1 reply#
> asterisk is inexplicably used both to declare a pointer and a COMPLETELY different operation of dereferencing a pointer.

This is the most confusing concept of pointers. I feel this could have been easily avoided with different character like ~ or ^ or other.

Continue this thread — 1 reply

Continue this thread — 4 more replies