The What, How, and Why of Void Pointers in C and C++?

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

The What, How, and Why of Void Pointers in C? // Pointers can be challenging for beginning programmers. And, void pointers add a level of perplexity. This view will take you through the basics, and hopefully straighten out any confusions about generic pointers, why we have them, and how they work in both C and C++.

***

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

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

Hearing that this is "a pointer VOID of any type information" was so enlightening.

SimGunther
Автор

When a friend asked me for a youtube channel that explains topics regarding C/C++, you were the first one that came in my mind. Keep up the good job!

wick
Автор

Although I tend to HATE C++, I do agree with being explicit. It shows intentionality. No, I didn't forget - I really wanted this to happen. It also allows for catching errors if you change types.

On the other hand - the "void" type is badly chosen (like many keywords in C, e.g. "static" for functions). It would have been a LOT better if void would have been expressed in "address units". It would also have made sense of "void pointer arithmetic".

HansBezemer
Автор

Thank god i found you. your videos are life saver, your so great. im a 24 year old Embedded software developer from india .happy coding

ajithanandhan
Автор

I haven't looked into void pointers until I watched your video. Just hearing the name "void pointer" always confused me because of my immediate thought void = nothing. But from your video I learned that I should think of it as "no type information" and that makes total sense now. Thanks a lot! I enjoy your channel A LOT!

DeniseNepraunig
Автор

Usually do not click the like button but this really helped me

cps
Автор

@Jacob
Thank!
you!
You're audio is way better today.

cleightthejw
Автор

One thing I keep seeing is people saying that void* isn't necessary anymore because of any non-C language feature, but clearly they've forgotten that old systems still exist that need their code maintained and new systems can benefit from a good programmer using C as opposed to using whatever bloated feature their language of choice implements. Optimization is a lot easier for a compiler to do if each instruction you type in correlates to just a few machine instructions, or in some cases one. If you add a bunch of fluff, it gets harder for the compiler to determine how best to optimize your code, and makes the compiler slower, and its output more bloated.

anon_y_mousse
Автор

Your content is really good! thank you so much

mariovrpereira
Автор

You can use it to code a form of dynamic programming. A void pointer could point to one of several different types, which type it is being set elsewhere in another var. (Often within the item pointed to).

andrewnorris
Автор

printf("%p") seems useful, thanks! I always use something like "%016llX", but then I have to know the size, cast to some large enough int type, etc. The only downside with "%p" is that the output seems to differ between compilers, but for quick debugging I guess this doesn't really matter.

TaleTN
Автор

Would you please explain why ((uint16_t*)p)++ is not allowed ? uint16_t being just a stand-in for any type. I have a mem block that i wanted to fill with various types (uint16 followed by uin32 etc.) and instead of doing many p + sizeof(x) statements i was trying to be concise by using this *(((uint16_t*)p)++) = 0xC0FF ; *(((uint32_t*)p)++) = 0xAAC0FFEE; but that's forbidden apparently.

FadyMegally
Автор

That's really a funny one, with 'feed beef', 'coffee' and 'dead beef'...😂

knofi
Автор

Could you talk about intptr_t? I have an application (Bezitopo) where an edge can be split in three, and I need to point to the three pieces, so I took an edge pointer, converted it to intptr_t, and put the piece number in the bottom two bits, which are always 0 if the word size is at least 32 bits.

pierreabbat
Автор

The best way to write C++ is to start each file with extern "C" {

ahmadhadwan
Автор

Would have been nice to touch on Variant return types, used to return one or more different types from a function. Other than a generic pointer I believe variant types are the best use for void*.

antonwalters
Автор

When you ran *make* at 1:46, it used *clang++* . But in 2:03 you ran *make* again (with no visible changes except for changing which file you're editing), and it used just *clang* .
Did you change the makefile and cut it out, or is it a weird *make* feature?

monochromeart
Автор

so...technically a void pointer is a pure memory pointer? It doesn't know or care, how it should read the data at that memory address. While a type pointer will try to interpret the memory as a specific type?

NikolaNevenov
Автор

I already knew all of this; I just like your videos.

When are you going to do a video on the worst parts of C that shouldn't exist: undefined behavior, and unspecified behavior?

andrewporter
Автор

I find void* interesting such as in an event callback system.
You can make a function pointer such as
(In c with using the "typedef" keyword) typedef void (*FunctionListener) (void*);
or (In c++ with the "using" keyword) using
Then you can pass a void function with the void* argument that will be casted into any DataType* within that function, where that same DataType* has been passed into that function.

You can also do something like void read_bytes(void* any_data, uintptr_t size); just to read bytes of any struct/class/datatype.

SpamTheHorse