CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2009
    Posts
    18

    Wink Storing pointer value in a string

    What happens when we store pointer value in the string, and convert back to pointer and deference the pointer. I know that, we should not convert pointer to a string and again convert back. Can you please explain why the pointer value should not be stored in a string.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Storing pointer value in a string

    Mind explaining why you want to store the numeric representation of an address as a string in the first place?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2009
    Posts
    18

    Re: Storing pointer value in a string

    I shall try to explain,

    I have a module, which accepts a callback function pointer ((e.g) void callback(string)) and a string argument. I would like to pass my object pointer to the string argument and when the callback gets called, I can convert the string back to pointer and access the object directly.

    I know it can't be done , but I don't understand the reason why? Can you please explain.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Storing pointer value in a string

    Off the top of my head I don't see why it wouldn't work. What happens when you try it?

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Storing pointer value in a string

    Quote Originally Posted by htonivi View Post
    I know it can't be done
    Who told you that? Academically it can...
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    template <typename T>
    std::string convertPointerToStringAddress(const T* obj)
    {
      int address(reinterpret_cast<int>(obj));
      std::stringstream ss;
      ss << address;
      return ss.str();
    }
    
    template <typename T>
    T* convertAddressStringToPointer(const std::string& address)
    {
      std::stringstream ss;
      ss << address;
      int tmp(0);
      if(!(ss >> tmp)) throw std::runtime_error("Failed - invalid address!");
      return reinterpret_cast<T*>(tmp);
    }
    
    int main()
    {
      int val(0);
      int* ptr = &val;
    
      std::cout << ptr << std::endl;
    
      std::string address = convertPointerToStringAddress(ptr);
      int* ptr2 = convertAddressStringToPointer<int>(address);
    
      if (ptr==ptr2)
      {
        std::cout << "The pointers are the same!" << std::endl;
      }
      else
      {  
        std::cout << "The pointers are different! : " << std::endl;
      }
      
      system("PAUSE");
    }
    But I would hate to see anything like the above in production code because it makes a mockery of type safety. If you "think" you need something like the above then I would suggest that your design is wrong and you need to rethink it.

  6. #6
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Storing pointer value in a string

    Quote Originally Posted by PredicateNormative View Post
    Who told you that? Academically it can...
    FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)...

  7. #7
    Join Date
    Mar 2009
    Posts
    18

    Re: Storing pointer value in a string

    It crashes intermittently. Please note that the object When I changed this logic, the program works fine.

  8. #8
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Storing pointer value in a string

    Quote Originally Posted by Plasmator View Post
    FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)...
    Thanks Plasmator. You are right - I have assumed that the two are of the same size, which although is true most of the time, it clearly is not true for LLP64 and LP64 data models. Therefore compilations under these 64 bit data models will experience issues when operating on addresses that map at values outside of 32 bits. On that note this code is clearly not portable and that is another reason for not using it in production code!

  9. #9
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Storing pointer value in a string

    Quote Originally Posted by htonivi View Post
    It crashes intermittently. Please note that the object When I changed this logic, the program works fine.
    What crashes intermittently?

  10. #10
    Join Date
    Mar 2009
    Posts
    18

    Re: Storing pointer value in a string

    I am sorry for my poor English. The program crashes intermittently. Please note that, When I changed the logic (not assigning pointer value to a string), the program works fine.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing pointer value in a string

    A better solution would be to create a functor which has a handle to the object internally as the callback. Boost::bind could make this easy.

  12. #12
    Join Date
    Mar 2009
    Posts
    18

    Re: Storing pointer value in a string

    Quote Originally Posted by Lindley View Post
    A better solution would be to create a functor which has a handle to the object internally as the callback. Boost::bind could make this easy.

    Thanks for the alternate solution. But I would like to know, if storing pointer value in string is the cause of the crash.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing pointer value in a string

    Could well be. I think going the "correct" route is more important than figuring out how bad the hack is though.
    Last edited by Lindley; March 9th, 2009 at 12:41 PM.

  14. #14
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Storing pointer value in a string

    Code:
    ObjectType * Object = new ObjectType();
    unsigned long i = &Object;   //make sure there is room, most pointers are 64-bit
    stringstream out;
    out << i;
    string s = out.str();
    
    ..
    
    sscanf(s.c_str(), "&#37;u", &i);
    
    ObjectType * newObjectPtr = (ObjectType *) i;
    Not really something you want to do, but you can. Also, the use of the C cast isn't recommended either.

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

Featured