why do void* pointers even exist?

preview_player
Показать описание
Why do void pointers exist? Why do they break our code? Should we even use them? In this video I talk about why void pointers are (not so) special and how to use them in your code.

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

Fun fact: When my dad was a kid, there were no void pointers in "K&R" C, they used to use char pointers. Void pointers were introduced in ANSI C

esra_erimez
Автор

homie’s using 8 column indent like an absolute savage

dickheadrecs
Автор

void* is type erasure. The machine doesnt know or care what a "bag of bits" represents, which is why malloc just returns a pointer to the beginning of a (now-valid if non-NULL) memory block, which the programmer then casts to specify how those bits are to be interpreted.

Considering again that the machine doesn't care, you can also freely read out the raw bytes of any variable by casting the base address of that variable to a char* pointer and reading out the bytes in a loop as long as you don't read past the sizeof that variable.

C is a very raw and low level language that treats the machine as the hardware that it really is. This leaves open enormous room for error but also gives you ultimate power over whatever you want to do.

metal
Автор

Void pointers are funded by big undefined behavior to sell more undefined behavior.

nordgaren
Автор

We had programming homework a few weeks ago involving void pointers. They're incredibly versatile and useful if you need an array or matrix that stores different data types in its cells rather than being the same data type on every cell.

letcreate
Автор

The malloc line only generates a warning if the .c file is compiled as C++. It's perfectly kosher to not cast in C - compile as C and you'll see...

johanngerell
Автор

Had a homework in my OS class where we used void pointers heavily to implement a fairly simple file system. It was great to lump data of files into a void pointer and put them into blocks within the FS and made data manipulation with memset relatively easy if you knew the offset calculations. Void pointers are an essential for any C programmer and are versatile when data type isn't an issue.

nallid
Автор

void* are great if you have a callback function in C. You pass one along with the function pointer as userdata and it comes back to you as a parameter in the callback ready to be casted since you know the shape(type) of the memory!

LuckieLordie
Автор

void * works like a generic type in C language. void * can be anything. You can create abstractions and let the user handle his data. curl library works that way.

fazemc
Автор

It's also very useful when you're dealing with callbacks. You can pass a void pointer (pointer to some arbitrary application data) that for example an external event library will hold on to and then pass as argument back to you when it's calling you back in the function you specified. Typically something like register_callback(callback_function, (void*)arbitrary_data); and at some later time the library calls The library can't know about your type so void * is useful here.

johalun
Автор

In C you are not required to cast void* to other pointer type. The call to malloc without casting the return is normal C code. It won't giver error nor warning. C++ is another story, in that language you indeed need to cast the return of malloc.

MatiasGRodriguez
Автор

At 2:13 you claim, that you could not pass the structure Person by value ... but this is actually wrong.
While C-language does not pass arrays by-value, it does indeed pass structures by value if you use a
struct variable rather than a struct * variable (as argument to a function). This of course will make the
code rather slow (and might have the side effect, that the called routine is only making changes in
the copy). However, my point is, C does indeed allow to pass structures by value.

Rai_Te
Автор

But you can totally pass something bigger than the word size by value, but it is probably passed via the stack instead of registers.

arf
Автор

5:45 In C++, this is correct. In C, I can’t get any of gcc, clang, or msvc to warn about that at *all.* People who code in C more than I do say casting the result of `malloc` is at best bad style and should probably be considered a code smell or even a bug, because C casts are powerful enough you don’t want to use them when you don’t have to, and void* can be implicitly converted to any pointer type.


I don’t like that at all, but I think it is true for C, which is part of why I stick to C++ when possible.

danielrhouck
Автор

at the 5:56 mark, you state that you need to cast the pointer type. It looks like you're using standard C, so that's not actually true. In C the void * type is compatible with all pointer types, so no casting is required. If, however, you're using a C++ compiler, because of the added type safety of the C++ language, you'll need to use a cast to your resulting pointer type, but if you're using C++ then you should probably be using new, and delete, and templates, to avoid the use of void *.

atheistlehman
Автор

Great content you've been putting out lately! I really appreciate you going over the core tricks and workings of C/ASM, especially since I just got my first job as a embedded dev. Helps out a ton, keep up the amazing work 🙏🙏

pavlebn
Автор

5:50 - this is incorrect. If you're writing C, you should not cast return value of malloc. Conversion to and from void pointer are implicit and in the case of malloc doing the cast may silence an issue of stdlib.h not being included (subject to C version).

And if you're writing C++ you should use new.

PS. A good idiom to use with malloc is to use sizeof with an expression rather than the type as in: "Person *person = malloc(sizeof *person);" This way, if you ever change the type of the variable, there's no risk that you forget to change the allocation size.

mina
Автор

I just took a job working on firmware and had no experience with it, whatsoever. This channel has been instrumental in helping me to understand how everything works. Thank you!

bestredditstories
Автор

Do not cast mallocs. In C, void* can be implicitly converted to and from any pointer type. It does not give errors, this is by design.

ex-xghh
Автор

Are you sure that at 5:45 the compiler will throw a warning? If am not wrong it will throw an error, but only with C++, it is totally fine to implicit covert a 'void *' to another kind of pointer in C.

stphano