I just made my first meaningful pull request to an actively developed FOSS project :D I implemented tap-to-reset for the wasp-os pedometer app.
It was a LOT of researching and finding out how the libraries work but I'm proud of what I was able to do...not knowing C or python XD
Anyone have some resources for someone who's used to loosely typed languages who's looking to get into C?
Python resources also accepted :3
@brandon For getting into C from that background, I'd implement a loosely-typed system with `enum`, `union` and `struct` (to create a “tagged union”), and do some stuff with it. Once you have enough basic competence with the language, you can do anything, and a project like that seems like it'd give you the competence you need.
Of course, you'd need to look stuff up, and a load of C resources are just plain wrong, so… Wikipedia and Wikibooks?
@brandon It's a while since I've done C, so I only remember the trivial bits, but:
• Writing int* x instead of int *x.
• Using unsafe standard library functions, or normal standard library functions in an unsafe way.
• Missing null checks.
• Casting the return value of `malloc`.
Stuff like that.
@wizzwizz4 What does it mean to cast?
Also, from what I know in Rust pretty sure int* x won't do anything?
@brandon Casting is saying “hey, pretend the bits representing *this* value mean *that* type!”
int *x = malloc(sizeof int);
@wizzwizz4 Oh god, looking at this really hurts...
Like, I barely understand it and I can tell this is just...why would you even
@brandon That is actually the correct way of doing it: best practices by all the standards I know.
I don't like C.
The way I understand this, you're dereferencing x, and casting it as an int, then making it point to memory location of the sizeof you just calculated.
So that's assigning the size of x to x?
It sounds like a pointless circular reference
@brandon Ah, that would be *(int *)x = malloc(sizeof(*x));
No, int *x = malloc(sizeof(*x)); declares a new variable x, with type `int *`, and assigns it a new bit of heap that's large enough to contain a single int.
@wizzwizz4 Is there a specific reason one would do this aside from reserving the space for the hell of it?
@brandon It's Rust's Box<int> (but without lifetimes). malloc'd memory survives a return, but regular stack variables don't.
@wizzwizz4 I mean sure but why not just put the thing that's supposed to go into memory instead of just reserving it for later?
@brandon Sure, you can do that!
*(int *)malloc(sizeof int) = 42;
There: we stored 42 in memory. Somewhere. No clue where, though.
@wizzwizz4 @efftoyz @brandon
malloc returns a void pointer (more or less the address of a collection of contiguous bytes from memory the size you specify.) C can't really do anything with a void pointer though so you have to tell the compiler what you want to treat that section of memory as (i.e. its type.) This can fail though as its asking your OS for the memory and the OS can say no for various reasons. In that case the function returns 0 (a.k.a. NULL) to indicate the allocation failed.
@brandon Some are scanned and on PDF format I believe. And the Python one I have is an ebook. I need to check.
@brandon for C, the best resource is still the K&R. It's a wonderful book!
Otherwise there's Modern C, written by one dude at INRIA and freely available online.
@rbd I really like the structure of the Modern C book. It makes it feel extremely approachable and even the "about this book" is humbling for some folks.
Thanks for the recommendation :3
Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.