> Generic C > Look inside > void* It's always like this, I swear.
@akirami12 жыл бұрын
this made cpp look comfortable to write :3 thanks
@FranksCreativeCorner Жыл бұрын
I learned generic linked lists last year at college. I had a great teacher so it was pretty easy to understand, but later on I started to realise some limitations. One doubt I had was "How do I make a printList function if I don't know what my data is?" I thought "Ideally my data is primitive (int, char), so I'll just have to change that %d to %something and stuff on the printf..." but that'd still imply making another printList function for each of those somethings, even with custom types, like persons or products (structs). In every single video I've seen on the internet about linked lists, all they'd do is create the node structure just for ints and the head would just be a double pointer to node. Apart from that, they'd do some basic functions and that'd be it. Not even once I've found a video about generic linked lists up until now. So the fact that not only you happen to implement generic lists in the same way I was taught, but found a way to make a GENERIC PRINTLIST FUNCTION, IN BARE C, just blows my mind. There are too many coincidences, my friend. This has got to be the God of Programming helping me with my college assignments. It's like I've finally unlocked the next level in depth of understanding of C syntax and it's possibilities! Putting jokes aside, not even watched the whole video, but I know for sure it'll be of great help. Ever since I learned generic lists I wanted to do this, so thank you tons man.
@theteachr Жыл бұрын
Thank you so much for such an elaborate comment, Franco! I’m planning to continue working on this in another stream. Implement a few more operations and possibly talk about Torvald’s Linked List with a good taste along the way.
@friedrichmyers7 ай бұрын
The video is good but his inexperience shows when he debugs lmao
@imranhasan871 Жыл бұрын
Please don't stop making videos please.
@friedrichmyers6 ай бұрын
These videos are very underrated for sure.
@killermonkey1392 Жыл бұрын
Hiya, a bit of advice on debugging your initial segfault: I would always advocate for using a debugger like GDB or tools like Valgrind (you added the -g flag for a reason), but if you choose to debug using printf, keep in mind that stdout might be buffered. This could mislead you on your search for the bug because a printf could be called, but then the segfault occurs before stdout is flushed, so you get no output! Therefore, you probably want to either print to stderr using fprintf(stderr, …) or manually flush stdout using fflush(stdout)
@theteachr Жыл бұрын
Thanks for the advice!
@friedrichmyers7 ай бұрын
When do we use Macros and when do we use Void pointers? I can create a macro and it will create things for me as well. But it will be different everytime.
@theteachr6 ай бұрын
Macros start to get unwieldy very fast (when there are skill issues). Like the moment they stop being a one liner. A function call will always result in better error messages (location of the error). Macros can expand to code referring to variables. This introduces a hidden dependency that the code block using it HAS to have a variable with the same name as in the macro, in scope.
@ElaineParra4 ай бұрын
Please keep adding videos about DS😊
@TheMachina4211 ай бұрын
Great video, but a better generic in C is to use uintptr_t, you can still contain any pointer you want, but you get the added benefit of also using it directly to store any value, not that you would want it, but it's more ergonomic than void * in my humble opinion, plus it makes for more explicit code. as when you use it you need to cast your pointer on your way in, and cast it back on your way out. Anyway great video a again :)
@theteachr11 ай бұрын
I concur. If I ever decide to drive this to completion, I will use uintptrs instead of void stars.
@abdullah58028 ай бұрын
What I would do is make generic functions that can operate on a linked list with any data type by using macros and such and let the user create the struct with the datatype he wants.
@brockdaniel884511 ай бұрын
Very interesting !
@mithrandirek18139 ай бұрын
Don't store a data pointer in node, store the data itself, let user alloc nodes with enough space
@mrglick50506 ай бұрын
You have to use a void* if you want generic data
@mithrandirek18136 ай бұрын
@@mrglick5050 not at all, just have the linkedlist (library) functions manage substructs of bigger structs which are alloced and freed by the user. This way the library functions see only the pointers, which they are supposed to manage. This way there isn't an unnecessary indirection through the void*. I'm mean, this is how it's done in the Linux kernel, so maybe look into it a bit more, you won't be disappointed
@mrglick50505 ай бұрын
@@mithrandirek1813 Ah, that sounds pretty smart! Will look into it for sure.
@Joee-Random2 жыл бұрын
Which is editor used for typing with git included ?
@theteachr2 жыл бұрын
I didn’t use any in this one, but I use a plugin on Neovim that makes it quick to stage and commit from within the editor. Emacs’ magit is claimed to be the best client for Git though.
@marka797011 ай бұрын
I made one version inspired from the video but it led to a cursed syntax where everything should be array or allocated on the heap. So I changed it a bit by adding a union and now is way cleaner without everything allocated on the heap. /*For example*/ typedef struct { union { float f32; int32_t i32; char str[STR_SIZE]; } data; void* next; void (*print)(void*); } NODE; add_front(&list, "World", str); float f32_num = 3.14; add_back(&list, &f32_num, f32);
@theteachr11 ай бұрын
Don’t you that managing the union will be a pain? If you’re going to release this as a library, the clients can’t use their types in your list. Or can they?
@marka797011 ай бұрын
@@theteachr You mean except primitive types ?
@matheusmotta5413 Жыл бұрын
Thanks man, it helped a lot.
@preoalex82984 ай бұрын
In C you should typedef your structs so that you dont write “struct” every time. typedef struct Node { void *data; Node *next; } Node; Node n = …;
@abrh1112 Жыл бұрын
hello sir, thank you for the video, i have a question, why we add data_size to GenericLinkedList and we didnt use it later.
@strongleongch8 ай бұрын
Cool video, but structs without typedef is hurting my soul 😅 You can do something like this: ``` typedef struct MyStruct { // fields } MyStruct; MyStruct *var = NULL; ``` If you need to reference type in itself, you must declare typesef separatly from declaring struct: ``` typedef struct Node Node; struct Node { Node *next; }; ``` Also naming struct is not nessecary (if you don't nest this struct): ``` typedef struct { // fields... } MyStruct; ```
@theteachr8 ай бұрын
Something I picked up from Linus. He despises typedeffing. You can read more on "linus on typedef". Maybe it'll soothe your hurt soul a little bit.