0

void variables?

I know the use of void pointers but havent seen a variable anywhere... what are they used for? Edit : please explain how void* is dangerous? **the one who downvoted is requested to specify the reason

25th Jun 2016, 4:17 PM
Mukul Kumar
Mukul Kumar - avatar
13 odpowiedzi
+ 1
You haven't seen any because they don't exist. "void" aka "empty / emptiness" refers to no type. This is why we use void as a return type if there is nothing to return. You probably wonder why void pointers are allowed, then. First back to the basics: Pointers are a variable that holds a memory address where another variable of a specific type is. An example: an int pointer is a variable which holds the memory address of an int. A void pointer is a variable that points to a memory address without a type. As void pointers lack the interpretation of what the memory at the stored address is, they can't easily be used as int pointers and others. This is why the compiler forces you to cast a void pointer into a typed pointer before you can work with it more easily.
25th Jun 2016, 4:41 PM
Stefan
Stefan - avatar
+ 1
That website is god of knowledge. ..... thanks..
25th Jun 2016, 5:05 PM
Mukul Kumar
Mukul Kumar - avatar
+ 1
I already did but maybe I presented it in a hard to understand way. void* is just pointing to a memory address. Fine, no problem. If you want to use it, you need to give the compiler an interpretation what kind of variable is at that memory address. Doing so involves knowledge about the actual type that is stored at the memory address. If you use void* as a parameter this information is "lost" when passing to the function and then is hard-coded into a reinterpret_cast in the function. This is severely error-prone. The void* might point to a double. The implementation of the function reinterpret_casts it to a uint32_t. This is a misinterpretation of the data (very likely resulting in garbage data). A misinterpretation might be the least of the problems you get. Apart from overwriting other data, segmentation faults and memory access violations are just around the corner... This might happen many times in the continuous software development process we have today and might be hard to find (only through proper code review or testing and even testing might not reveal some of the problems). By using templates the type to which the pointer points is not lost. The same is true for Boost.Any (that is also using templates but might be a bit more convenient). Therefore the compiler will either give you warnings or errors, if you try to convert or use the data in a wrong way. I hope this helped.
26th Jun 2016, 1:31 PM
Stefan
Stefan - avatar
+ 1
thanks stefan....I understand now
27th Jun 2016, 7:26 AM
Mukul Kumar
Mukul Kumar - avatar
0
but I tried and the turbo c++ makes and compiles a void variable.... (to my surprise)
25th Jun 2016, 4:43 PM
Mukul Kumar
Mukul Kumar - avatar
0
You can still cast a variable to void which will tell the compiler that you don't use this variable (it's a best practice for unused parameters of the "main" function). Anyways, there are multiple options as to why one might think that. Maybe it's a misunderstanding of the code or your compiler allows for more than others. See http://stackoverflow.com/questions/25853258/why-cant-we-declare-a-variable-of-type-void Stackoverflow is an awesome resource :-)
25th Jun 2016, 5:02 PM
Stefan
Stefan - avatar
0
1. You can use them as function parameters 2. You can use void *to point any type
26th Jun 2016, 7:04 AM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
_Geometry dash_ _Roh_ (AKA NovaRate0315) - avatar
0
As my initial answer may have understated what you can actually do with void*, I want to incorporate NovaRate0315s answer. As I already stated in my initial answer, void* can be used to point to an arbitrary memory address. This can be used to pass a memory address to other functions which then have to cast this void* into a type. Nevertheless this is *very* insecure and can be considered a bad practice (always reinterpret_cast involved). If you want to work with a pointer of flexible type, pls use templates or the "any" class in the Boost library (look for Boost.Any). Templates and Boost.Any enable you to keep the type and therefore still have the type checks of the compiler. This will make your program less error-prone.
26th Jun 2016, 12:30 PM
Stefan
Stefan - avatar
0
HEY!!! do you know what is a handle??? well all microsoft is using handles in windows a window application is not possible without using atleast one handle. I dont think so that working with void* is a bad practice
26th Jun 2016, 12:39 PM
Mukul Kumar
Mukul Kumar - avatar
0
what about DirextX?
26th Jun 2016, 1:05 PM
Mukul Kumar
Mukul Kumar - avatar
0
Again, no example helps the case. Just because DirectX uses it does not mean it's an acceptable practice. This is not about how others do it, this is about how *should* I or everyone do it. If you can point out weaknesses in my argumentation, pls do so. I'm eager to learn.
26th Jun 2016, 1:11 PM
Stefan
Stefan - avatar
0
I edited my question please answer
26th Jun 2016, 1:17 PM
Mukul Kumar
Mukul Kumar - avatar
- 1
Mukul, I know what a handle is. Also, your answer is a bit Microsoft specific. Furthermore, the API you refer to is a C API (as all operating system API that I know) and not a C++ API. Also, you did not argue my actual point that this is error-prone and in C++ there are better ways to do the same thing. In every language you should leverage the means the language gives you to write code that is less error-prone / safe and readable. Just because something exists doesn't mean it's a good idea to use it. If you can't avoid it, you have to but in any other way avoid this.
26th Jun 2016, 12:55 PM
Stefan
Stefan - avatar