Results 1 to 4 of 4

Thread: how print a void pointer in string way?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    how print a void pointer in string way?

    see these code:
    Code:
    int a=200;
    void *b=&a;
    how can i print the the *b in string way?

    Code:
    cout << *static_cast<string*>(b); //gives me an error memory
    Code:
    cout << *static_cast<char*>(b);//gives me only 1 character(like the tables characters, in ASCII table)
    why that 2 exemples give me strange results?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how print a void pointer in string way?

    Why would you want a pointer to the string's underlying type? Or why would you do this in the first place? You're likely to receive a result that is not going to be null terminated at any 'proper' point in memory, and there's other issues with this as well.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: how print a void pointer in string way?

    Quote Originally Posted by AceInfinity View Post
    Why would you want a pointer to the string's underlying type? Or why would you do this in the first place? You're likely to receive a result that is not going to be null terminated at any 'proper' point in memory, and there's other issues with this as well.
    i want build my own any type. instead the boost::any. and use it without use the static_cast<> or other.
    but tell me, please: why i only get 1 character instead the number?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: how print a void pointer in string way?

    Doing so is much more complex than you think, which is suggestive by how you're demonstrating a conversion between types here. You have to provide the conversion yourself. What number did you use? You know strings are null terminated correct? If you are thinking that this *should* work, then you are not in a position to be creating your own any_type wrapper in my opinion. This is a complex enough and daunting task to achieve with a reasonable level of quality, even for an experienced programmer. Making it portable, fully type safe, etc... I don't see the point. You probably won't end up with a very efficient wrapper in the end, and it would be something ineffective for use 99.9% of the time, and perhaps only used by the lazy programmers out there, even if you did create such a thing. The overhead of having such a wrapper as well--I'm sure most developed programmers would avoid using it as much as possible and just write implementations that they need.
    Last edited by AceInfinity; Dec 9th, 2013 at 01:53 AM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width