Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:35+00:00 2026-05-25T09:31:35+00:00

Is void* necessary apart from memory allocation related stuff in C++? Can you give

  • 0

Is void* necessary apart from memory allocation related stuff in C++?
Can you give me an example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:31:36+00:00Added an answer on May 25, 2026 at 9:31 am

    Logging memory addresses

    If you want to output a pointer using iostreams (e.g. for logging) then going via void* is the only way of ensuring operator<< hasn’t been overloaded in some crazy way.

    #include <iostream>
    
    struct foo {
    };
    
    std::ostream& operator<<(std::ostream& out, foo*) {
      return out<<"it's a trap!";
    }
    
    int main() {
      foo bar;
      foo *ptr = &bar;
    
      std::cout << ptr << std::endl;
      std::cout << static_cast<void*>(ptr) << std::endl;
    }
    

    Testing iostream status

    iostreams overload operator void* as a status check so that syntax like if (stream) or while (stream) is a short hand way of testing the stream status.


    Template meta programming

    You might want to use void* with template metaprogramming sometimes as a reduced catch all, e.g. with SFINAE tricks, but more often than not there’s a nicer way around it using a partial specialisation of one form or another.


    Accessing most derived pointer

    As Alf pointed out in the comments dynamic_cast<void*> is also useful for getting at the most derived type in a heirarchy, e.g.:

    #include <iostream>
    
    struct other {
      virtual void func() = 0;
      int c;
    };
    
    struct foo {
      virtual void func() { std::cout << "foo" << std::endl; }
      int a;
    };
    
    struct bar : foo, other {
      virtual void func() { std::cout << "bar" << std::endl; }
      int b;
    };
    
    namespace {
      void f(foo *ptr) {
        ptr->func();
        std::cout << ptr << std::endl;
        std::cout << dynamic_cast<void*>(ptr) << std::endl;
      }
    
      void g(other *ptr) {
        ptr->func();
        std::cout << ptr << std::endl;
        std::cout << dynamic_cast<void*>(ptr) << std::endl;
      }
    }
    
    int main() {
      foo a;
      bar b;
      f(&a);
      f(&b);
      g(&b);
    }
    

    Gives:

    foo
    0xbfb815f8
    0xbfb815f8
    bar
    0xbfb815e4
    0xbfb815e4
    bar
    0xbfb815ec
    0xbfb815e4
    

    On my system.


    Exceptions

    § 15.3.1 states:

    The exception-declaration shall not denote a pointer or reference to
    an incomplete type, other than void*, const void*, volatile void*, or
    const volatile void*.

    So it seems to be the only legal way of catching a pointer to an incomplete type is via void*. (Although I think there’s possibly bigger issues if you actually needed to use that)


    Legacy C uses

    There are a lot of “legacy” C uses for void* for storing pointers to data without knowing what it is, but in new C++ code there is almost always a better way of expressing the same functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is SaveChanges() necessary with function imports (stored procedures)? Example: void foo(Product product) { //
void (int a[]) { a[5] = 3; // this is wrong? } Can I
Is it really necessary to implement all the methods to a subclass(inherited from a
I know unsubscription from event IS necessary. My questions comes from the generated code:
Consider this code: #include <vector> void Example() { std::vector<TCHAR*> list; TCHAR* pLine = new
For embedded applications, it is often necessary to access fixed memory locations for peripheral
Is it necessary to implement a BST with both keys and values? I can
for example: void FilterA(Hashset<T> blackList, List<T> myList) { blackList.UnionWith(myList); } I don't know if
Consider an example (which compiles in java) public abstract interface Interface { public void
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.