Make your Data Type more Abstract with Opaque Types in C

preview_player
Показать описание
---

Make your Data Type more Abstract with Opaque Types in C // When you think about abstract data types (ADTs) you probably think about object-oriented languages, like C++, java, ruby, or python. But, even though C isn't object-oriented and doesn't have a private keyword, you can still limit data type visibility using opaque types, in the same way that LibC does with FILE pointers. This video shows you how.

Related Videos:



***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

I have used opaque types in C for years, but I didn't know that they where called opaque types until today. Thanks

tordjarv
Автор

Thank you for explaining not only what opaque types are and how to implement them, but also why one might want to use them. I've only just begun learning C, but you've helped me to appreciate C for what it is and to leave my OO baggage behind.

ISKLEMMI
Автор

Honestly your videos are the best! Keep posting!

samuelmartin
Автор

This is perfect thank you. Top of my OS class thanks to all these. We NEVER learn anything about writing good C!

mattwilliams
Автор

To me, this is the best pattern I learned for C. This makes everything better!

LDdrums
Автор

One reason that incomplete data types are allowed is that they are sometimes necessary. You might have two structs that reference each other with pointers, and then you must first put an incomplete declaration of one of the structs, then a complete declaration of the other, and lastly the complete declaration of the first one.

mdperpe
Автор

Excellent explanation. They key was the forward declaration of the struct

pathayes
Автор

hi, instead of using 2 malloc (one for meta data and data), i'd suggest using array inside the allocated data

typedef struct {
int size;
int num_entries;
int head;
int tail;
int values[0];
} queue;

you can allocate them using

queue * q = malloc(sizeof(queue) + max_size);

the structure can be free using single free, just suggestion, i usually use this myself

kuriel
Автор

Heard a lot about incomplete data types but your video explains perfectly. Thanks.

praveen
Автор

Can you make a video on polymorphism and casting a pointer of one struct type into another like we do `struct sockaddr_in` to `struct sockaddr` and other places and how pointer casting between structs can give us polymorphism behavior in C? It will be really helpful if you can make a video on this specific topic. Anyways, amazing video as always :-)

mockingbird
Автор

Very nice video, thank you.
I'd like to see a video on virtual function tables on structs. I am a Go programmer and it's convenient having function members that tell me exactly what my "object/struct-instance" can do simply by typing a period. In Go, struct methods are just syntactic sugar for "mystruct.mymethod(&mystruct, somevar);". However in Go, "&mystruct" is passed implicitly into the function if it is a pointer receiver. Go doesn't have inheritance either, and IMO it's very similar to C. I am new to C but after watching a few of your videos I wrote a simple Vector struct but I wanted to emulate go so it worked like:
Vector* my_vector = new_vector(100);
my_vector->add(my_vector, 1);
etc...

Neeseius
Автор

And there's a step more on top of this. Using a handle or a void * to the struct so you don't even expose the struct type. Really handy for libraries.

LDdrums
Автор

Very well explained ! First time on this channel and instant subsciption !

dallas_barr
Автор

Thank you very much for teaching C to us!

otmanm
Автор

I'd like to see a video about benefits of making header files standalone-compilable, so they contain all other headers required to ensure the header can be compiled on its own, and why this is a good thing (or downsides, if there are any).

ChrisBNisbet
Автор

I've heard a few compelling arguments about not abusing typedefs and polluting the global namespace. I prefer to typedef only in the .c where it can help to clean up some of the verbosity of the implementation. But in the .h keep the "interface" clear on exactly what is what.

nexusclarum
Автор

Thank you, sir! This is exactly what I'm waiting for!

duyanhpham
Автор

you don't need the second typedef, just the struct definition

zxuiji
Автор

I love a channel. Thanks Jacob, u are the man.

tapiocaferoz
Автор

I have a couple of questions:
1. Do opaque types have to be a pointer/address of that struct?
2. Is there anyone to declare “public” variables in the .h but still have other “private” variables declared in the .c file?

samuelmartin
join shbcf.ru