   Void Pointers: Is it C or C++ stuff?
  Plainly, I'm just asking.I have a book called "C Programming: A Modern Approach", by K. N. King, but it didn't say anything about {void *} stuffs. (Quotation mark doesn't look nice.)If it's part of C, I have high hopes my professor is willing to teach me about it.If it's part of C++, my professor will ignore me for a while.Reason: Out of Curriculum.   
  I don't see anything there you're asking.EDIT: ah, in the subject line. void* exists in both languages, but it's barely used at all in C++, particularly in good C++. It's mostly been replaced by templates and functors. void* does not make for good code, in any language.   ;  void* can be used in c and c++. Using void* can be a bit tricky sometimes but can also be handy and sometimes faster and cleaner than using the safer alternative - templates.Using void* in no way shows bad code or anything like that, if used correctly with the knowledge when and where to use them.Using anything out of place or the wrong way is a sign of bad programming, not using a specific thing, when it is used the right way.Don't be afraid to look into void* and see for yourself when they can be used and are used.;Quote:Original post by RusenecUsing void* in no way shows bad code or anything like that, if used correctly with the knowledge when and where to use them.It does. Even when used correctly it's bad code. It may be necessary bad code; particularly when interfacing with legacy C libraries it often comes in handy. But the purpose of void* is to remove the type safety guarantees that the language provides you, and as a result code which uses void* starts out less safe than code which doesn't. The compiler won't help you get it right, and the problems which arise from getting it wrong are often subtle and difficult to find. Necessity often takes a programmer into bad code. A good programmer is one who finds fewer of these necessities.Quote:Using void* can be a bit tricky sometimes but can also be handy and sometimes faster and cleaner than using the safer alternative - templates."Clean" sounds like a value judgment on coding style, so I won't really dispute that, but "fast" seems like quite a stretch. For instance, qsort, which uses void*s because of the lack of templates in C, is inefficient compared to std::sort due to the use of void*s instead of templates. I'd be hard pressed to think of an example where it would be faster... can you?   ;  You need to deal with void* if you overload the memory allocation and release functions.void* operator new(size_t number_of_bytes) throw (std::bad_alloc);void* operator new[]   (size_t number_of_bytes) throw (std::bad_alloc);void  operator delete  (void* address)throw ();void  operator delete[](void* address)throw ();;  Put more simply a (void*) is simply a pointer that can be used to point to anything. It is essentially typeless and can be cast to any type. To answer your original question it is very much C.  C++ is a superset of C and contains all of C's features.   ;Quote:Original post by SneftelI'd be hard pressed to think of an example where it would be faster... can you?I know that the boost library has used void* in a few places; I can't recall why. Of course, there is always the argument of code size. There are possibly cases where using a void*-based implementation outperforms multiple template-generated code-paths because the former is more cache/memory efficient.[Edited for better wording][Edited by - Konfusius on May 18, 2010 1:25:42 PM];Quote:Original post by SneftelEDIT: ah, in the subject line. void* exists in both languages, but it's barely used at all in C++, particularly in good C++. It's mostly been replaced by templates and functors. void* does not make for good code, in any language. D: I think CUDA requires void* for it's memory allocation. D:   ;Quote:Original post by KonfusiusI know that the boost library has used void* in a few places;I believe they mostly use it in stuff like PIMPL implementations, to try and reduce some code bloat.  But they only tend to do it in places where the type is ultimately still known, so they aren't throwing the type safety out the window.   ;  To repeat everything previously said: void* exists in C, and by extension C++. It's not used very often as templates are often more effective a solution: Easier for programmers to visualise, as well as providing more compile-time error support.Good or Bad code is arguably a bit of a silly discussion: There is only effective and ineffective. Solutions which work well are to be kept, solutions which don't solve effectively/at all, and/or open up new potential errors, are discarded and/or modified as needed.void* can work well in highly specific situations, and be an effective solution, but in a lot of situations more overall-effective alternatives exist and should be used.void* is very useful when you can be guaranteed to know the type of something, but templates do not provide a flexible enough solution. They are still often very much a last resort, due to the vulnerabilities they expose.For example, this is a situation where void*'s become useful, and I have a variant of this code "on file" which uses the same principles, and mixes in some C++0x features, to work for both member functions and static functions and more generically:http://www.codeproject.com/KB/cpp/ImpossiblyFastCppDelegate.aspx[Edited by - MrMorley on May 18, 2010 12:16:20 PM]