Stroing pointer value in a string

Mar 10, 2009 at 1:29am
The program crashes intermittently, when I store pointer value in the string, and convert back to pointer and deference the pointer. Can anyone help me understand the reason for crashing and let me know what could be the issues/defects of storing pointer value in a string.
Mar 10, 2009 at 1:41am
Code would help.

Are you trying something like a bad cast (casting string to char* instead of using string::c_str).

This code accomplishes what you describe just fine:

1
2
3
4
5
6
7
8
9
char* cstr = "test";

std::string str;

str = cstr;

const char* back = str.c_str();

std::cout << back;


However 'back' can only be used for a short window of time (until 'str' loses scope or is modified)
Last edited on Mar 10, 2009 at 1:42am
Mar 10, 2009 at 1:52am
Let me 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.


Mar 10, 2009 at 2:20am
I'm not following at all. I mean I get the callback, but "I would like to pass my object pointer to the string argument" doesn't make much sense to me (you can't pass an object as a string unless the object is a string).

Can't you post a simple code snippit to illustrate?
Mar 10, 2009 at 2:35am
Let me try to explain with the sample code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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");
}


The sample code works perfectly fine, but the program crashes intermittently, can you let me the know the reason for the same.

I would like to know, if it is advisable to store the pointer value to a string (as I don't want to change the interface of the module).
Mar 10, 2009 at 4:17am
I believe it is because in convertAddressStringToPointer (btw, choose shorter function names XD), you are returning a pointer to a temporary variable which no longer exists when the function is done.
Mar 10, 2009 at 12:30pm
The first hint that your program has a bug should be the need for reinterpret_cast, since you are basically saying that you are smarter than the compiler.

If you are going to be completely type-unsafe, why not just pass a void* to the callback function and then cast it to a pointer to the correct type?

Topic archived. No new replies allowed.