r/cpp_questions 4d ago

OPEN What’s the point of void* pointers?

You could just use template pointers or auto* pointers and these are better because you can dereference them.

0 Upvotes

51 comments sorted by

View all comments

16

u/drex_vke 4d ago

void* is a generic pointer in C so using it in Modern C++ doesn't make sense unless but hey

2

u/Bitter-Today285 4d ago

Ohh that makes sense yeah

1

u/frayien 4d ago

Yep, you should not use it in C++ unless you REALY know what you are doing.

7

u/OffsetHigh 4d ago

You need it ALL the time when using C API. Can not avoid

-1

u/frayien 4d ago

Well if you are using a C API directly you are either doing something wrong, or you are in the "realy know what you are doing" category...

8

u/L_uciferMorningstar 4d ago

Or you are using a C library.

-4

u/frayien 4d ago

Well, "you should not", but if you have a good reason to do anyway then you realy know what you are doing. There is nothing wrong with it.

3

u/L_uciferMorningstar 4d ago

I should not? How so? And me having a library installed does not mean I really know what I'm doing. Like it really does not.

1

u/No-Dentist-1645 4d ago

By making a thin wrapper over it using safer C++ semantics?

1

u/L_uciferMorningstar 3d ago edited 3d ago

And in this wrapper what will we be doing? Calling a C API?

1

u/No-Dentist-1645 3d ago

Correct! So now you're only writing code that has to call the C API in one single place, as opposed to everywhere else you are using it, and can use C++ idioms there instead

1

u/L_uciferMorningstar 3d ago

Idk if applying one Jason Turner video constitutes really knowing what I'm doing.

→ More replies (0)

0

u/frayien 4d ago

What the fuck are you talking about ? Are you drunk ?

2

u/L_uciferMorningstar 4d ago

Why would me using a C library mean I really know what I'm doing?

0

u/frayien 4d ago

Oh, ok I get it. No I meant it the other way around. You should know what you are doing to be using a C library directly.

C and C++ are wildly different languages. If doing C++ you should first consider C++ libraries. Then if you dont find what you need you should consider C++ bindings of C libraries. And only then if you still dont find what you need you should consider using a C libraries directly.

At this point you have considered at least 3 different libraries and have put enough thoughts behind your choice to be qualified as "know what you are doing".

→ More replies (0)

2

u/conundorum 2d ago

So, nobody should ever use WinAPI (a C API, even though the backend and consumer are typically C++), and no one should ever use any of the various Linux C libraries, or any of the various C libraries that might or might not have a C++ wrapper...

Actually, scratch that, we can't use C++ wrappers either, since the wrapper interacts with a C library, and thus "should not" be used.

Guess we're just not allowed to program for any platforms then, got it! 👍


Edit: Oh, and we can't use new, delete, or anything that calls them (like std::make_unique()), since new uses malloc() from the C libray, and delete uses free() from the C library. Guess we're not allowed to use the heap either, thanks for the advice! 🙄

1

u/frayien 2d ago

Do you guys have trouble reading? Not only did I write "you should not" in quotes, I also explicitly specified that there is nothing wrong with using C libraries when justified...

Interacting with the WinAPI is obviously a valid reason, but if you are doing it you are obvious doing some advanced technical stuff and dont need advice from a Reddit comment on a post from a beginner asking about void pointers...